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" |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 30 | #include "gc/accounting/heap_bitmap-inl.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" |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame^] | 34 | #include "gc/reference_processor.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 35 | #include "gc/space/image_space.h" |
| 36 | #include "gc/space/large_object_space.h" |
| 37 | #include "gc/space/space-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 38 | #include "mark_sweep-inl.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 39 | #include "mirror/art_field-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 40 | #include "mirror/object-inl.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 41 | #include "runtime.h" |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 42 | #include "scoped_thread_state_change.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 43 | #include "thread-inl.h" |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 44 | #include "thread_list.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 45 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 46 | using ::art::mirror::ArtField; |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 47 | using ::art::mirror::Class; |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 48 | using ::art::mirror::Object; |
| 49 | using ::art::mirror::ObjectArray; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 50 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 51 | namespace art { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 52 | namespace gc { |
| 53 | namespace collector { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 54 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 55 | // Performance options. |
Mathieu Chartier | eb7bbad | 2014-02-25 15:52:46 -0800 | [diff] [blame] | 56 | static constexpr bool kUseRecursiveMark = false; |
| 57 | static constexpr bool kUseMarkStackPrefetch = true; |
| 58 | static constexpr size_t kSweepArrayChunkFreeSize = 1024; |
| 59 | static constexpr bool kPreCleanCards = true; |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 60 | |
| 61 | // Parallelism options. |
Mathieu Chartier | eb7bbad | 2014-02-25 15:52:46 -0800 | [diff] [blame] | 62 | static constexpr bool kParallelCardScan = true; |
| 63 | static constexpr bool kParallelRecursiveMark = true; |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 64 | // Don't attempt to parallelize mark stack processing unless the mark stack is at least n |
| 65 | // elements. This is temporary until we reduce the overhead caused by allocating tasks, etc.. Not |
| 66 | // having this can add overhead in ProcessReferences since we may end up doing many calls of |
| 67 | // ProcessMarkStack with very small mark stacks. |
Mathieu Chartier | eb7bbad | 2014-02-25 15:52:46 -0800 | [diff] [blame] | 68 | static constexpr size_t kMinimumParallelMarkStackSize = 128; |
| 69 | static constexpr bool kParallelProcessMarkStack = true; |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 70 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 71 | // Profiling and information flags. |
Mathieu Chartier | eb7bbad | 2014-02-25 15:52:46 -0800 | [diff] [blame] | 72 | static constexpr bool kProfileLargeObjects = false; |
| 73 | static constexpr bool kMeasureOverhead = false; |
| 74 | static constexpr bool kCountTasks = false; |
| 75 | static constexpr bool kCountJavaLangRefs = false; |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 76 | static constexpr bool kCountMarkedObjects = false; |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 77 | |
| 78 | // Turn off kCheckLocks when profiling the GC since it slows the GC down by up to 40%. |
Mathieu Chartier | eb7bbad | 2014-02-25 15:52:46 -0800 | [diff] [blame] | 79 | static constexpr bool kCheckLocks = kDebugLocking; |
Mathieu Chartier | 7bf9f19 | 2014-04-04 11:09:41 -0700 | [diff] [blame] | 80 | static constexpr bool kVerifyRootsMarked = kIsDebugBuild; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 81 | |
Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 82 | // If true, revoke the rosalloc thread-local buffers at the |
| 83 | // checkpoint, as opposed to during the pause. |
| 84 | static constexpr bool kRevokeRosAllocThreadLocalBuffersAtCheckpoint = true; |
| 85 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 86 | void MarkSweep::BindBitmaps() { |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 87 | timings_.StartSplit("BindBitmaps"); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 88 | WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 89 | // Mark all of the spaces we never collect as immune. |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 90 | for (const auto& space : GetHeap()->GetContinuousSpaces()) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 91 | if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyNeverCollect) { |
Mathieu Chartier | 8d56210 | 2014-03-12 17:42:10 -0700 | [diff] [blame] | 92 | CHECK(immune_region_.AddContinuousSpace(space)) << "Failed to add space " << *space; |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 93 | } |
| 94 | } |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 95 | timings_.EndSplit(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 98 | MarkSweep::MarkSweep(Heap* heap, bool is_concurrent, const std::string& name_prefix) |
| 99 | : GarbageCollector(heap, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 100 | name_prefix + |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 101 | (is_concurrent ? "concurrent mark sweep": "mark sweep")), |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 102 | gc_barrier_(new Barrier(0)), |
Mathieu Chartier | 958291c | 2013-08-27 18:14:55 -0700 | [diff] [blame] | 103 | mark_stack_lock_("mark sweep mark stack lock", kMarkSweepMarkStackLock), |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 104 | is_concurrent_(is_concurrent) { |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void MarkSweep::InitializePhase() { |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 108 | TimingLogger::ScopedSplit split("InitializePhase", &timings_); |
Mathieu Chartier | e53225c | 2013-08-19 10:59:11 -0700 | [diff] [blame] | 109 | mark_stack_ = heap_->mark_stack_.get(); |
| 110 | DCHECK(mark_stack_ != nullptr); |
Mathieu Chartier | 8d56210 | 2014-03-12 17:42:10 -0700 | [diff] [blame] | 111 | immune_region_.Reset(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 112 | class_count_ = 0; |
| 113 | array_count_ = 0; |
| 114 | other_count_ = 0; |
| 115 | large_object_test_ = 0; |
| 116 | large_object_mark_ = 0; |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 117 | overhead_time_ = 0; |
| 118 | work_chunks_created_ = 0; |
| 119 | work_chunks_deleted_ = 0; |
| 120 | reference_count_ = 0; |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 121 | mark_null_count_ = 0; |
| 122 | mark_immune_count_ = 0; |
| 123 | mark_fastpath_count_ = 0; |
| 124 | mark_slowpath_count_ = 0; |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 125 | { |
| 126 | // TODO: I don't think we should need heap bitmap lock to get the mark bitmap. |
| 127 | ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 128 | mark_bitmap_ = heap_->GetMarkBitmap(); |
| 129 | } |
Hiroshi Yamauchi | df386c5 | 2014-04-08 16:21:52 -0700 | [diff] [blame] | 130 | if (!clear_soft_references_) { |
| 131 | // Always clear soft references if a non-sticky collection. |
| 132 | clear_soft_references_ = GetGcType() != collector::kGcTypeSticky; |
| 133 | } |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void MarkSweep::RunPhases() { |
| 137 | Thread* self = Thread::Current(); |
| 138 | InitializePhase(); |
| 139 | Locks::mutator_lock_->AssertNotHeld(self); |
| 140 | if (IsConcurrent()) { |
| 141 | GetHeap()->PreGcVerification(this); |
| 142 | { |
| 143 | ReaderMutexLock mu(self, *Locks::mutator_lock_); |
| 144 | MarkingPhase(); |
| 145 | } |
| 146 | ScopedPause pause(this); |
| 147 | GetHeap()->PrePauseRosAllocVerification(this); |
| 148 | PausePhase(); |
| 149 | RevokeAllThreadLocalBuffers(); |
| 150 | } else { |
| 151 | ScopedPause pause(this); |
| 152 | GetHeap()->PreGcVerificationPaused(this); |
| 153 | MarkingPhase(); |
| 154 | GetHeap()->PrePauseRosAllocVerification(this); |
| 155 | PausePhase(); |
| 156 | RevokeAllThreadLocalBuffers(); |
| 157 | } |
| 158 | { |
| 159 | // Sweeping always done concurrently, even for non concurrent mark sweep. |
| 160 | ReaderMutexLock mu(self, *Locks::mutator_lock_); |
| 161 | ReclaimPhase(); |
| 162 | } |
| 163 | GetHeap()->PostGcVerification(this); |
| 164 | FinishPhase(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | void MarkSweep::ProcessReferences(Thread* self) { |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 168 | TimingLogger::ScopedSplit split("ProcessReferences", &timings_); |
Mathieu Chartier | 8e56c7e | 2012-11-20 13:25:50 -0800 | [diff] [blame] | 169 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame^] | 170 | GetHeap()->GetReferenceProcessor()->ProcessReferences( |
| 171 | true, &timings_, clear_soft_references_, &IsMarkedCallback, &MarkObjectCallback, |
| 172 | &ProcessMarkStackCallback, this); |
Mathieu Chartier | 1ad2784 | 2014-03-19 17:08:17 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Mathieu Chartier | 0f7bf6a | 2014-03-28 10:05:39 -0700 | [diff] [blame] | 175 | void MarkSweep::PausePhase() { |
| 176 | TimingLogger::ScopedSplit split("(Paused)PausePhase", &timings_); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 177 | Thread* self = Thread::Current(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 178 | Locks::mutator_lock_->AssertExclusiveHeld(self); |
Mathieu Chartier | 0f7bf6a | 2014-03-28 10:05:39 -0700 | [diff] [blame] | 179 | if (IsConcurrent()) { |
| 180 | // Handle the dirty objects if we are a concurrent GC. |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 181 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 182 | // Re-mark root set. |
| 183 | ReMarkRoots(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 184 | // 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] | 185 | RecursiveMarkDirtyObjects(true, accounting::CardTable::kCardDirty); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 186 | } |
Mathieu Chartier | 0f7bf6a | 2014-03-28 10:05:39 -0700 | [diff] [blame] | 187 | { |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 188 | TimingLogger::ScopedSplit split("SwapStacks", &timings_); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 189 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 0f7bf6a | 2014-03-28 10:05:39 -0700 | [diff] [blame] | 190 | heap_->SwapStacks(self); |
| 191 | live_stack_freeze_size_ = heap_->GetLiveStack()->Size(); |
| 192 | // Need to revoke all the thread local allocation stacks since we just swapped the allocation |
| 193 | // stacks and don't want anybody to allocate into the live stack. |
Mathieu Chartier | c22c59e | 2014-02-24 15:16:06 -0800 | [diff] [blame] | 194 | RevokeAllThreadLocalAllocationStacks(self); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 195 | } |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 196 | timings_.StartSplit("PreSweepingGcVerification"); |
| 197 | heap_->PreSweepingGcVerification(this); |
| 198 | timings_.EndSplit(); |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 199 | // Disallow new system weaks to prevent a race which occurs when someone adds a new system |
| 200 | // weak before we sweep them. Since this new system weak may not be marked, the GC may |
| 201 | // incorrectly sweep it. This also fixes a race where interning may attempt to return a strong |
| 202 | // reference to a string that is about to be swept. |
| 203 | Runtime::Current()->DisallowNewSystemWeaks(); |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame^] | 204 | // Enable the reference processing slow path, needs to be done with mutators paused since there |
| 205 | // is no lock in the GetReferent fast path. |
| 206 | GetHeap()->GetReferenceProcessor()->EnableSlowPath(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 207 | } |
| 208 | |
Mathieu Chartier | dda54f5 | 2014-02-24 09:58:40 -0800 | [diff] [blame] | 209 | void MarkSweep::PreCleanCards() { |
| 210 | // Don't do this for non concurrent GCs since they don't have any dirty cards. |
| 211 | if (kPreCleanCards && IsConcurrent()) { |
| 212 | Thread* self = Thread::Current(); |
| 213 | CHECK(!Locks::mutator_lock_->IsExclusiveHeld(self)); |
| 214 | // Process dirty cards and add dirty cards to mod union tables, also ages cards. |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 215 | heap_->ProcessCards(timings_, false); |
Mathieu Chartier | eb7bbad | 2014-02-25 15:52:46 -0800 | [diff] [blame] | 216 | // The checkpoint root marking is required to avoid a race condition which occurs if the |
| 217 | // following happens during a reference write: |
| 218 | // 1. mutator dirties the card (write barrier) |
| 219 | // 2. GC ages the card (the above ProcessCards call) |
| 220 | // 3. GC scans the object (the RecursiveMarkDirtyObjects call below) |
| 221 | // 4. mutator writes the value (corresponding to the write barrier in 1.) |
| 222 | // This causes the GC to age the card but not necessarily mark the reference which the mutator |
| 223 | // wrote into the object stored in the card. |
| 224 | // Having the checkpoint fixes this issue since it ensures that the card mark and the |
| 225 | // reference write are visible to the GC before the card is scanned (this is due to locks being |
| 226 | // acquired / released in the checkpoint code). |
| 227 | // The other roots are also marked to help reduce the pause. |
Mathieu Chartier | 0f7bf6a | 2014-03-28 10:05:39 -0700 | [diff] [blame] | 228 | MarkRootsCheckpoint(self, false); |
Mathieu Chartier | dda54f5 | 2014-02-24 09:58:40 -0800 | [diff] [blame] | 229 | MarkNonThreadRoots(); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 230 | MarkConcurrentRoots( |
| 231 | static_cast<VisitRootFlags>(kVisitRootFlagClearRootLog | kVisitRootFlagNewRoots)); |
Mathieu Chartier | dda54f5 | 2014-02-24 09:58:40 -0800 | [diff] [blame] | 232 | // Process the newly aged cards. |
| 233 | RecursiveMarkDirtyObjects(false, accounting::CardTable::kCardDirty - 1); |
| 234 | // TODO: Empty allocation stack to reduce the number of objects we need to test / mark as live |
| 235 | // in the next GC. |
| 236 | } |
| 237 | } |
| 238 | |
Mathieu Chartier | c22c59e | 2014-02-24 15:16:06 -0800 | [diff] [blame] | 239 | void MarkSweep::RevokeAllThreadLocalAllocationStacks(Thread* self) { |
| 240 | if (kUseThreadLocalAllocationStack) { |
Mathieu Chartier | 0f7bf6a | 2014-03-28 10:05:39 -0700 | [diff] [blame] | 241 | timings_.NewSplit("RevokeAllThreadLocalAllocationStacks"); |
Mathieu Chartier | c22c59e | 2014-02-24 15:16:06 -0800 | [diff] [blame] | 242 | Locks::mutator_lock_->AssertExclusiveHeld(self); |
| 243 | heap_->RevokeAllThreadLocalAllocationStacks(self); |
| 244 | } |
| 245 | } |
| 246 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 247 | void MarkSweep::MarkingPhase() { |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 248 | TimingLogger::ScopedSplit split("MarkingPhase", &timings_); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 249 | Thread* self = Thread::Current(); |
| 250 | |
| 251 | BindBitmaps(); |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 252 | FindDefaultSpaceBitmap(); |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 253 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 254 | // Process dirty cards and add dirty cards to mod union tables. |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 255 | heap_->ProcessCards(timings_, false); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 256 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 257 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 258 | MarkRoots(self); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 259 | MarkReachableObjects(); |
Mathieu Chartier | dda54f5 | 2014-02-24 09:58:40 -0800 | [diff] [blame] | 260 | // Pre-clean dirtied cards to reduce pauses. |
| 261 | PreCleanCards(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 264 | void MarkSweep::UpdateAndMarkModUnion() { |
| 265 | for (const auto& space : heap_->GetContinuousSpaces()) { |
Mathieu Chartier | 8d56210 | 2014-03-12 17:42:10 -0700 | [diff] [blame] | 266 | if (immune_region_.ContainsSpace(space)) { |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 267 | const char* name = space->IsZygoteSpace() ? "UpdateAndMarkZygoteModUnionTable" : |
| 268 | "UpdateAndMarkImageModUnionTable"; |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 269 | TimingLogger::ScopedSplit split(name, &timings_); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 270 | accounting::ModUnionTable* mod_union_table = heap_->FindModUnionTableFromSpace(space); |
| 271 | CHECK(mod_union_table != nullptr); |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 272 | mod_union_table->UpdateAndMarkReferences(MarkHeapReferenceCallback, this); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 277 | void MarkSweep::MarkReachableObjects() { |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 278 | UpdateAndMarkModUnion(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 279 | // Recursively mark all the non-image bits set in the mark bitmap. |
| 280 | RecursiveMark(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | void MarkSweep::ReclaimPhase() { |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 284 | TimingLogger::ScopedSplit split("ReclaimPhase", &timings_); |
Mathieu Chartier | 720ef76 | 2013-08-17 14:46:54 -0700 | [diff] [blame] | 285 | Thread* self = Thread::Current(); |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame^] | 286 | // Process the references concurrently. |
| 287 | ProcessReferences(self); |
Mathieu Chartier | 0f7bf6a | 2014-03-28 10:05:39 -0700 | [diff] [blame] | 288 | SweepSystemWeaks(self); |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 289 | Runtime::Current()->AllowNewSystemWeaks(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 290 | { |
| 291 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 292 | |
| 293 | // Reclaim unmarked objects. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 294 | Sweep(false); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 295 | |
| 296 | // Swap the live and mark bitmaps for each space which we modified space. This is an |
| 297 | // optimization that enables us to not clear live bits inside of the sweep. Only swaps unbound |
| 298 | // bitmaps. |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 299 | timings_.StartSplit("SwapBitmaps"); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 300 | SwapBitmaps(); |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 301 | timings_.EndSplit(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 302 | |
| 303 | // Unbind the live and mark bitmaps. |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 304 | TimingLogger::ScopedSplit split("UnBindBitmaps", &timings_); |
| 305 | GetHeap()->UnBindBitmaps(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 306 | } |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 307 | } |
| 308 | |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 309 | void MarkSweep::FindDefaultSpaceBitmap() { |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 310 | TimingLogger::ScopedSplit split("FindDefaultMarkBitmap", &timings_); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 311 | for (const auto& space : GetHeap()->GetContinuousSpaces()) { |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 312 | accounting::ContinuousSpaceBitmap* bitmap = space->GetMarkBitmap(); |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 313 | // We want to have the main space instead of non moving if possible. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 314 | if (bitmap != nullptr && |
| 315 | space->GetGcRetentionPolicy() == space::kGcRetentionPolicyAlwaysCollect) { |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 316 | current_space_bitmap_ = bitmap; |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 317 | // If we are not the non moving space exit the loop early since this will be good enough. |
| 318 | if (space != heap_->GetNonMovingSpace()) { |
| 319 | break; |
| 320 | } |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 321 | } |
| 322 | } |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 323 | if (current_space_bitmap_ == nullptr) { |
| 324 | heap_->DumpSpaces(); |
| 325 | LOG(FATAL) << "Could not find a default mark bitmap"; |
| 326 | } |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Mathieu Chartier | ac86a7c | 2012-11-12 15:03:16 -0800 | [diff] [blame] | 329 | void MarkSweep::ExpandMarkStack() { |
Mathieu Chartier | ba311b4 | 2013-08-27 13:02:30 -0700 | [diff] [blame] | 330 | ResizeMarkStack(mark_stack_->Capacity() * 2); |
| 331 | } |
| 332 | |
| 333 | void MarkSweep::ResizeMarkStack(size_t new_size) { |
Mathieu Chartier | ac86a7c | 2012-11-12 15:03:16 -0800 | [diff] [blame] | 334 | // Rare case, no need to have Thread::Current be a parameter. |
Mathieu Chartier | ac86a7c | 2012-11-12 15:03:16 -0800 | [diff] [blame] | 335 | if (UNLIKELY(mark_stack_->Size() < mark_stack_->Capacity())) { |
| 336 | // Someone else acquired the lock and expanded the mark stack before us. |
| 337 | return; |
| 338 | } |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 339 | std::vector<Object*> temp(mark_stack_->Begin(), mark_stack_->End()); |
Mathieu Chartier | ba311b4 | 2013-08-27 13:02:30 -0700 | [diff] [blame] | 340 | CHECK_LE(mark_stack_->Size(), new_size); |
| 341 | mark_stack_->Resize(new_size); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 342 | for (const auto& obj : temp) { |
| 343 | mark_stack_->PushBack(obj); |
Mathieu Chartier | ac86a7c | 2012-11-12 15:03:16 -0800 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 347 | inline void MarkSweep::MarkObjectNonNullParallel(Object* obj) { |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 348 | DCHECK(obj != nullptr); |
Mathieu Chartier | ac86a7c | 2012-11-12 15:03:16 -0800 | [diff] [blame] | 349 | if (MarkObjectParallel(obj)) { |
Mathieu Chartier | ba311b4 | 2013-08-27 13:02:30 -0700 | [diff] [blame] | 350 | MutexLock mu(Thread::Current(), mark_stack_lock_); |
| 351 | if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) { |
Mathieu Chartier | 184e322 | 2013-08-03 14:02:57 -0700 | [diff] [blame] | 352 | ExpandMarkStack(); |
Mathieu Chartier | ac86a7c | 2012-11-12 15:03:16 -0800 | [diff] [blame] | 353 | } |
Mathieu Chartier | ba311b4 | 2013-08-27 13:02:30 -0700 | [diff] [blame] | 354 | // The object must be pushed on to the mark stack. |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 355 | mark_stack_->PushBack(obj); |
Mathieu Chartier | ac86a7c | 2012-11-12 15:03:16 -0800 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
Mathieu Chartier | 3bb57c7 | 2014-02-18 11:38:45 -0800 | [diff] [blame] | 359 | mirror::Object* MarkSweep::MarkObjectCallback(mirror::Object* obj, void* arg) { |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 360 | MarkSweep* mark_sweep = reinterpret_cast<MarkSweep*>(arg); |
| 361 | mark_sweep->MarkObject(obj); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 362 | return obj; |
| 363 | } |
| 364 | |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 365 | void MarkSweep::MarkHeapReferenceCallback(mirror::HeapReference<mirror::Object>* ref, void* arg) { |
| 366 | reinterpret_cast<MarkSweep*>(arg)->MarkObject(ref->AsMirrorPtr()); |
| 367 | } |
| 368 | |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 369 | class MarkSweepMarkObjectSlowPath { |
| 370 | public: |
| 371 | explicit MarkSweepMarkObjectSlowPath(MarkSweep* mark_sweep) : mark_sweep_(mark_sweep) { |
| 372 | } |
| 373 | |
| 374 | void operator()(const Object* obj) const ALWAYS_INLINE { |
| 375 | if (kProfileLargeObjects) { |
| 376 | // TODO: Differentiate between marking and testing somehow. |
| 377 | ++mark_sweep_->large_object_test_; |
| 378 | ++mark_sweep_->large_object_mark_; |
| 379 | } |
| 380 | space::LargeObjectSpace* large_object_space = mark_sweep_->GetHeap()->GetLargeObjectsSpace(); |
| 381 | if (UNLIKELY(!IsAligned<kPageSize>(obj) || |
| 382 | (kIsDebugBuild && !large_object_space->Contains(obj)))) { |
| 383 | LOG(ERROR) << "Tried to mark " << obj << " not contained by any spaces"; |
| 384 | LOG(ERROR) << "Attempting see if it's a bad root"; |
| 385 | mark_sweep_->VerifyRoots(); |
| 386 | LOG(FATAL) << "Can't mark invalid object"; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | private: |
| 391 | MarkSweep* const mark_sweep_; |
| 392 | }; |
| 393 | |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 394 | inline void MarkSweep::MarkObjectNonNull(Object* obj) { |
| 395 | DCHECK(obj != nullptr); |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 396 | if (kUseBakerOrBrooksReadBarrier) { |
| 397 | // Verify all the objects have the correct pointer installed. |
| 398 | obj->AssertReadBarrierPointer(); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 399 | } |
Mathieu Chartier | 8d56210 | 2014-03-12 17:42:10 -0700 | [diff] [blame] | 400 | if (immune_region_.ContainsObject(obj)) { |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 401 | if (kCountMarkedObjects) { |
| 402 | ++mark_immune_count_; |
| 403 | } |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 404 | DCHECK(mark_bitmap_->Test(obj)); |
| 405 | } else if (LIKELY(current_space_bitmap_->HasAddress(obj))) { |
| 406 | if (kCountMarkedObjects) { |
| 407 | ++mark_fastpath_count_; |
| 408 | } |
| 409 | if (UNLIKELY(!current_space_bitmap_->Set(obj))) { |
| 410 | PushOnMarkStack(obj); // This object was not previously marked. |
| 411 | } |
| 412 | } else { |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 413 | if (kCountMarkedObjects) { |
| 414 | ++mark_slowpath_count_; |
| 415 | } |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 416 | MarkSweepMarkObjectSlowPath visitor(this); |
| 417 | // TODO: We already know that the object is not in the current_space_bitmap_ but MarkBitmap::Set |
| 418 | // will check again. |
| 419 | if (!mark_bitmap_->Set(obj, visitor)) { |
| 420 | PushOnMarkStack(obj); // Was not already marked, push. |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 421 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 425 | inline void MarkSweep::PushOnMarkStack(Object* obj) { |
| 426 | if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) { |
| 427 | // Lock is not needed but is here anyways to please annotalysis. |
| 428 | MutexLock mu(Thread::Current(), mark_stack_lock_); |
| 429 | ExpandMarkStack(); |
| 430 | } |
| 431 | // The object must be pushed on to the mark stack. |
| 432 | mark_stack_->PushBack(obj); |
| 433 | } |
| 434 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 435 | inline bool MarkSweep::MarkObjectParallel(const Object* obj) { |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 436 | DCHECK(obj != nullptr); |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 437 | if (kUseBakerOrBrooksReadBarrier) { |
| 438 | // Verify all the objects have the correct pointer installed. |
| 439 | obj->AssertReadBarrierPointer(); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 440 | } |
Mathieu Chartier | 8d56210 | 2014-03-12 17:42:10 -0700 | [diff] [blame] | 441 | if (immune_region_.ContainsObject(obj)) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 442 | DCHECK(IsMarked(obj)); |
| 443 | return false; |
| 444 | } |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 445 | // Try to take advantage of locality of references within a space, failing this find the space |
| 446 | // the hard way. |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 447 | accounting::ContinuousSpaceBitmap* object_bitmap = current_space_bitmap_; |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 448 | if (LIKELY(object_bitmap->HasAddress(obj))) { |
| 449 | return !object_bitmap->AtomicTestAndSet(obj); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 450 | } |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 451 | MarkSweepMarkObjectSlowPath visitor(this); |
| 452 | return !mark_bitmap_->AtomicTestAndSet(obj, visitor); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 455 | // Used to mark objects when processing the mark stack. If an object is null, it is not marked. |
| 456 | inline void MarkSweep::MarkObject(Object* obj) { |
| 457 | if (obj != nullptr) { |
Mathieu Chartier | 9642c96 | 2013-08-05 17:40:36 -0700 | [diff] [blame] | 458 | MarkObjectNonNull(obj); |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 459 | } else if (kCountMarkedObjects) { |
| 460 | ++mark_null_count_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 464 | void MarkSweep::MarkRootParallelCallback(Object** root, void* arg, uint32_t /*thread_id*/, |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 465 | RootType /*root_type*/) { |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 466 | reinterpret_cast<MarkSweep*>(arg)->MarkObjectNonNullParallel(*root); |
Mathieu Chartier | ac86a7c | 2012-11-12 15:03:16 -0800 | [diff] [blame] | 467 | } |
| 468 | |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 469 | void MarkSweep::VerifyRootMarked(Object** root, void* arg, uint32_t /*thread_id*/, |
| 470 | RootType /*root_type*/) { |
| 471 | CHECK(reinterpret_cast<MarkSweep*>(arg)->IsMarked(*root)); |
| 472 | } |
| 473 | |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 474 | void MarkSweep::MarkRootCallback(Object** root, void* arg, uint32_t /*thread_id*/, |
| 475 | RootType /*root_type*/) { |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 476 | reinterpret_cast<MarkSweep*>(arg)->MarkObjectNonNull(*root); |
| 477 | } |
| 478 | |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 479 | void MarkSweep::VerifyRootCallback(const Object* root, void* arg, size_t vreg, |
Mathieu Chartier | 7bf9f19 | 2014-04-04 11:09:41 -0700 | [diff] [blame] | 480 | const StackVisitor* visitor, RootType root_type) { |
| 481 | reinterpret_cast<MarkSweep*>(arg)->VerifyRoot(root, vreg, visitor, root_type); |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Mathieu Chartier | 7bf9f19 | 2014-04-04 11:09:41 -0700 | [diff] [blame] | 484 | void MarkSweep::VerifyRoot(const Object* root, size_t vreg, const StackVisitor* visitor, |
| 485 | RootType root_type) { |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 486 | // See if the root is on any space bitmap. |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 487 | if (heap_->GetLiveBitmap()->GetContinuousSpaceBitmap(root) == nullptr) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 488 | space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
Mathieu Chartier | 4202b74 | 2012-10-17 17:51:25 -0700 | [diff] [blame] | 489 | if (!large_object_space->Contains(root)) { |
Mathieu Chartier | 7bf9f19 | 2014-04-04 11:09:41 -0700 | [diff] [blame] | 490 | LOG(ERROR) << "Found invalid root: " << root << " with type " << root_type; |
Ian Rogers | 40e3bac | 2012-11-20 00:09:14 -0800 | [diff] [blame] | 491 | if (visitor != NULL) { |
| 492 | LOG(ERROR) << visitor->DescribeLocation() << " in VReg: " << vreg; |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | void MarkSweep::VerifyRoots() { |
| 499 | Runtime::Current()->GetThreadList()->VerifyRoots(VerifyRootCallback, this); |
| 500 | } |
| 501 | |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 502 | void MarkSweep::MarkRoots(Thread* self) { |
| 503 | if (Locks::mutator_lock_->IsExclusiveHeld(self)) { |
| 504 | // If we exclusively hold the mutator lock, all threads must be suspended. |
| 505 | timings_.StartSplit("MarkRoots"); |
| 506 | Runtime::Current()->VisitRoots(MarkRootCallback, this); |
| 507 | timings_.EndSplit(); |
| 508 | RevokeAllThreadLocalAllocationStacks(self); |
| 509 | } else { |
Mathieu Chartier | 0f7bf6a | 2014-03-28 10:05:39 -0700 | [diff] [blame] | 510 | MarkRootsCheckpoint(self, kRevokeRosAllocThreadLocalBuffersAtCheckpoint); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 511 | // At this point the live stack should no longer have any mutators which push into it. |
| 512 | MarkNonThreadRoots(); |
| 513 | MarkConcurrentRoots( |
| 514 | static_cast<VisitRootFlags>(kVisitRootFlagAllRoots | kVisitRootFlagStartLoggingNewRoots)); |
| 515 | } |
Mathieu Chartier | 9ebae1f | 2012-10-15 17:38:16 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 518 | void MarkSweep::MarkNonThreadRoots() { |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 519 | timings_.StartSplit("MarkNonThreadRoots"); |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 520 | Runtime::Current()->VisitNonThreadRoots(MarkRootCallback, this); |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 521 | timings_.EndSplit(); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 522 | } |
| 523 | |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 524 | void MarkSweep::MarkConcurrentRoots(VisitRootFlags flags) { |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 525 | timings_.StartSplit("MarkConcurrentRoots"); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 526 | // Visit all runtime roots and clear dirty flags. |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 527 | Runtime::Current()->VisitConcurrentRoots(MarkRootCallback, this, flags); |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 528 | timings_.EndSplit(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 531 | class ScanObjectVisitor { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 532 | public: |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 533 | explicit ScanObjectVisitor(MarkSweep* const mark_sweep) ALWAYS_INLINE |
| 534 | : mark_sweep_(mark_sweep) {} |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 535 | |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 536 | void operator()(Object* obj) const ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 537 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 538 | if (kCheckLocks) { |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 539 | Locks::mutator_lock_->AssertSharedHeld(Thread::Current()); |
| 540 | Locks::heap_bitmap_lock_->AssertExclusiveHeld(Thread::Current()); |
| 541 | } |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 542 | mark_sweep_->ScanObject(obj); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | private: |
| 546 | MarkSweep* const mark_sweep_; |
| 547 | }; |
| 548 | |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 549 | class DelayReferenceReferentVisitor { |
| 550 | public: |
| 551 | explicit DelayReferenceReferentVisitor(MarkSweep* collector) : collector_(collector) { |
| 552 | } |
| 553 | |
| 554 | void operator()(mirror::Class* klass, mirror::Reference* ref) const |
| 555 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 556 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
| 557 | collector_->DelayReferenceReferent(klass, ref); |
| 558 | } |
| 559 | |
| 560 | private: |
| 561 | MarkSweep* const collector_; |
| 562 | }; |
| 563 | |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 564 | template <bool kUseFinger = false> |
| 565 | class MarkStackTask : public Task { |
| 566 | public: |
| 567 | MarkStackTask(ThreadPool* thread_pool, MarkSweep* mark_sweep, size_t mark_stack_size, |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 568 | Object** mark_stack) |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 569 | : mark_sweep_(mark_sweep), |
| 570 | thread_pool_(thread_pool), |
| 571 | mark_stack_pos_(mark_stack_size) { |
| 572 | // We may have to copy part of an existing mark stack when another mark stack overflows. |
| 573 | if (mark_stack_size != 0) { |
| 574 | DCHECK(mark_stack != NULL); |
| 575 | // TODO: Check performance? |
| 576 | std::copy(mark_stack, mark_stack + mark_stack_size, mark_stack_); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 577 | } |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 578 | if (kCountTasks) { |
| 579 | ++mark_sweep_->work_chunks_created_; |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | static const size_t kMaxSize = 1 * KB; |
| 584 | |
| 585 | protected: |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 586 | class MarkObjectParallelVisitor { |
| 587 | public: |
| 588 | explicit MarkObjectParallelVisitor(MarkStackTask<kUseFinger>* chunk_task, |
| 589 | MarkSweep* mark_sweep) ALWAYS_INLINE |
| 590 | : chunk_task_(chunk_task), mark_sweep_(mark_sweep) {} |
| 591 | |
| 592 | void operator()(Object* obj, MemberOffset offset, bool /* static */) const ALWAYS_INLINE |
| 593 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 594 | mirror::Object* ref = obj->GetFieldObject<mirror::Object>(offset); |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 595 | if (ref != nullptr && mark_sweep_->MarkObjectParallel(ref)) { |
| 596 | if (kUseFinger) { |
| 597 | android_memory_barrier(); |
| 598 | if (reinterpret_cast<uintptr_t>(ref) >= |
| 599 | static_cast<uintptr_t>(mark_sweep_->atomic_finger_)) { |
| 600 | return; |
| 601 | } |
| 602 | } |
| 603 | chunk_task_->MarkStackPush(ref); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | private: |
| 608 | MarkStackTask<kUseFinger>* const chunk_task_; |
| 609 | MarkSweep* const mark_sweep_; |
| 610 | }; |
| 611 | |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 612 | class ScanObjectParallelVisitor { |
| 613 | public: |
| 614 | explicit ScanObjectParallelVisitor(MarkStackTask<kUseFinger>* chunk_task) ALWAYS_INLINE |
| 615 | : chunk_task_(chunk_task) {} |
| 616 | |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 617 | // No thread safety analysis since multiple threads will use this visitor. |
| 618 | void operator()(Object* obj) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 619 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
| 620 | MarkSweep* const mark_sweep = chunk_task_->mark_sweep_; |
| 621 | MarkObjectParallelVisitor mark_visitor(chunk_task_, mark_sweep); |
| 622 | DelayReferenceReferentVisitor ref_visitor(mark_sweep); |
| 623 | mark_sweep->ScanObjectVisit(obj, mark_visitor, ref_visitor); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | private: |
| 627 | MarkStackTask<kUseFinger>* const chunk_task_; |
| 628 | }; |
| 629 | |
| 630 | virtual ~MarkStackTask() { |
| 631 | // Make sure that we have cleared our mark stack. |
| 632 | DCHECK_EQ(mark_stack_pos_, 0U); |
| 633 | if (kCountTasks) { |
| 634 | ++mark_sweep_->work_chunks_deleted_; |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | MarkSweep* const mark_sweep_; |
| 639 | ThreadPool* const thread_pool_; |
| 640 | // Thread local mark stack for this task. |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 641 | Object* mark_stack_[kMaxSize]; |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 642 | // Mark stack position. |
| 643 | size_t mark_stack_pos_; |
| 644 | |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 645 | void MarkStackPush(Object* obj) ALWAYS_INLINE { |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 646 | if (UNLIKELY(mark_stack_pos_ == kMaxSize)) { |
| 647 | // Mark stack overflow, give 1/2 the stack to the thread pool as a new work task. |
| 648 | mark_stack_pos_ /= 2; |
| 649 | auto* task = new MarkStackTask(thread_pool_, mark_sweep_, kMaxSize - mark_stack_pos_, |
| 650 | mark_stack_ + mark_stack_pos_); |
| 651 | thread_pool_->AddTask(Thread::Current(), task); |
| 652 | } |
| 653 | DCHECK(obj != nullptr); |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 654 | DCHECK_LT(mark_stack_pos_, kMaxSize); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 655 | mark_stack_[mark_stack_pos_++] = obj; |
| 656 | } |
| 657 | |
| 658 | virtual void Finalize() { |
| 659 | delete this; |
| 660 | } |
| 661 | |
| 662 | // Scans all of the objects |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 663 | virtual void Run(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 664 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 665 | ScanObjectParallelVisitor visitor(this); |
| 666 | // TODO: Tune this. |
| 667 | static const size_t kFifoSize = 4; |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 668 | BoundedFifoPowerOfTwo<Object*, kFifoSize> prefetch_fifo; |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 669 | for (;;) { |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 670 | Object* obj = nullptr; |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 671 | if (kUseMarkStackPrefetch) { |
| 672 | while (mark_stack_pos_ != 0 && prefetch_fifo.size() < kFifoSize) { |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 673 | Object* obj = mark_stack_[--mark_stack_pos_]; |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 674 | DCHECK(obj != nullptr); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 675 | __builtin_prefetch(obj); |
| 676 | prefetch_fifo.push_back(obj); |
| 677 | } |
| 678 | if (UNLIKELY(prefetch_fifo.empty())) { |
| 679 | break; |
| 680 | } |
| 681 | obj = prefetch_fifo.front(); |
| 682 | prefetch_fifo.pop_front(); |
| 683 | } else { |
| 684 | if (UNLIKELY(mark_stack_pos_ == 0)) { |
| 685 | break; |
| 686 | } |
| 687 | obj = mark_stack_[--mark_stack_pos_]; |
| 688 | } |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 689 | DCHECK(obj != nullptr); |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 690 | visitor(obj); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 691 | } |
| 692 | } |
| 693 | }; |
| 694 | |
| 695 | class CardScanTask : public MarkStackTask<false> { |
| 696 | public: |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 697 | CardScanTask(ThreadPool* thread_pool, MarkSweep* mark_sweep, |
| 698 | accounting::ContinuousSpaceBitmap* bitmap, |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 699 | byte* begin, byte* end, byte minimum_age, size_t mark_stack_size, |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 700 | Object** mark_stack_obj) |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 701 | : MarkStackTask<false>(thread_pool, mark_sweep, mark_stack_size, mark_stack_obj), |
| 702 | bitmap_(bitmap), |
| 703 | begin_(begin), |
| 704 | end_(end), |
| 705 | minimum_age_(minimum_age) { |
| 706 | } |
| 707 | |
| 708 | protected: |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 709 | accounting::ContinuousSpaceBitmap* const bitmap_; |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 710 | byte* const begin_; |
| 711 | byte* const end_; |
| 712 | const byte minimum_age_; |
| 713 | |
| 714 | virtual void Finalize() { |
| 715 | delete this; |
| 716 | } |
| 717 | |
| 718 | virtual void Run(Thread* self) NO_THREAD_SAFETY_ANALYSIS { |
| 719 | ScanObjectParallelVisitor visitor(this); |
| 720 | accounting::CardTable* card_table = mark_sweep_->GetHeap()->GetCardTable(); |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 721 | 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] | 722 | VLOG(heap) << "Parallel scanning cards " << reinterpret_cast<void*>(begin_) << " - " |
| 723 | << reinterpret_cast<void*>(end_) << " = " << cards_scanned; |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 724 | // Finish by emptying our local mark stack. |
| 725 | MarkStackTask::Run(self); |
| 726 | } |
| 727 | }; |
| 728 | |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 729 | size_t MarkSweep::GetThreadCount(bool paused) const { |
| 730 | if (heap_->GetThreadPool() == nullptr || !heap_->CareAboutPauseTimes()) { |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 731 | return 1; |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 732 | } |
| 733 | if (paused) { |
| 734 | return heap_->GetParallelGCThreadCount() + 1; |
| 735 | } else { |
| 736 | return heap_->GetConcGCThreadCount() + 1; |
| 737 | } |
| 738 | } |
| 739 | |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 740 | void MarkSweep::ScanGrayObjects(bool paused, byte minimum_age) { |
| 741 | accounting::CardTable* card_table = GetHeap()->GetCardTable(); |
| 742 | ThreadPool* thread_pool = GetHeap()->GetThreadPool(); |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 743 | size_t thread_count = GetThreadCount(paused); |
| 744 | // The parallel version with only one thread is faster for card scanning, TODO: fix. |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 745 | if (kParallelCardScan && thread_count > 1) { |
Mathieu Chartier | 720ef76 | 2013-08-17 14:46:54 -0700 | [diff] [blame] | 746 | Thread* self = Thread::Current(); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 747 | // Can't have a different split for each space since multiple spaces can have their cards being |
| 748 | // scanned at the same time. |
| 749 | timings_.StartSplit(paused ? "(Paused)ScanGrayObjects" : "ScanGrayObjects"); |
| 750 | // Try to take some of the mark stack since we can pass this off to the worker tasks. |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 751 | Object** mark_stack_begin = mark_stack_->Begin(); |
| 752 | Object** mark_stack_end = mark_stack_->End(); |
Mathieu Chartier | 720ef76 | 2013-08-17 14:46:54 -0700 | [diff] [blame] | 753 | const size_t mark_stack_size = mark_stack_end - mark_stack_begin; |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 754 | // Estimated number of work tasks we will create. |
| 755 | const size_t mark_stack_tasks = GetHeap()->GetContinuousSpaces().size() * thread_count; |
| 756 | DCHECK_NE(mark_stack_tasks, 0U); |
| 757 | const size_t mark_stack_delta = std::min(CardScanTask::kMaxSize / 2, |
| 758 | mark_stack_size / mark_stack_tasks + 1); |
| 759 | for (const auto& space : GetHeap()->GetContinuousSpaces()) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 760 | if (space->GetMarkBitmap() == nullptr) { |
| 761 | continue; |
| 762 | } |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 763 | byte* card_begin = space->Begin(); |
| 764 | byte* card_end = space->End(); |
Hiroshi Yamauchi | 0941b04 | 2013-11-05 11:34:03 -0800 | [diff] [blame] | 765 | // Align up the end address. For example, the image space's end |
| 766 | // may not be card-size-aligned. |
| 767 | card_end = AlignUp(card_end, accounting::CardTable::kCardSize); |
| 768 | DCHECK(IsAligned<accounting::CardTable::kCardSize>(card_begin)); |
| 769 | DCHECK(IsAligned<accounting::CardTable::kCardSize>(card_end)); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 770 | // Calculate how many bytes of heap we will scan, |
| 771 | const size_t address_range = card_end - card_begin; |
| 772 | // Calculate how much address range each task gets. |
| 773 | const size_t card_delta = RoundUp(address_range / thread_count + 1, |
| 774 | accounting::CardTable::kCardSize); |
| 775 | // Create the worker tasks for this space. |
| 776 | while (card_begin != card_end) { |
| 777 | // Add a range of cards. |
| 778 | size_t addr_remaining = card_end - card_begin; |
| 779 | size_t card_increment = std::min(card_delta, addr_remaining); |
| 780 | // Take from the back of the mark stack. |
| 781 | size_t mark_stack_remaining = mark_stack_end - mark_stack_begin; |
| 782 | size_t mark_stack_increment = std::min(mark_stack_delta, mark_stack_remaining); |
| 783 | mark_stack_end -= mark_stack_increment; |
| 784 | mark_stack_->PopBackCount(static_cast<int32_t>(mark_stack_increment)); |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 785 | DCHECK_EQ(mark_stack_end, mark_stack_->End()); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 786 | // Add the new task to the thread pool. |
| 787 | auto* task = new CardScanTask(thread_pool, this, space->GetMarkBitmap(), card_begin, |
| 788 | card_begin + card_increment, minimum_age, |
| 789 | mark_stack_increment, mark_stack_end); |
| 790 | thread_pool->AddTask(self, task); |
| 791 | card_begin += card_increment; |
| 792 | } |
| 793 | } |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 794 | |
Hiroshi Yamauchi | 0941b04 | 2013-11-05 11:34:03 -0800 | [diff] [blame] | 795 | // Note: the card scan below may dirty new cards (and scan them) |
| 796 | // as a side effect when a Reference object is encountered and |
| 797 | // queued during the marking. See b/11465268. |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 798 | thread_pool->SetMaxActiveWorkers(thread_count - 1); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 799 | thread_pool->StartWorkers(self); |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 800 | thread_pool->Wait(self, true, true); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 801 | thread_pool->StopWorkers(self); |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 802 | timings_.EndSplit(); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 803 | } else { |
| 804 | for (const auto& space : GetHeap()->GetContinuousSpaces()) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 805 | if (space->GetMarkBitmap() != nullptr) { |
| 806 | // Image spaces are handled properly since live == marked for them. |
| 807 | switch (space->GetGcRetentionPolicy()) { |
| 808 | case space::kGcRetentionPolicyNeverCollect: |
| 809 | timings_.StartSplit(paused ? "(Paused)ScanGrayImageSpaceObjects" : |
| 810 | "ScanGrayImageSpaceObjects"); |
| 811 | break; |
| 812 | case space::kGcRetentionPolicyFullCollect: |
| 813 | timings_.StartSplit(paused ? "(Paused)ScanGrayZygoteSpaceObjects" : |
| 814 | "ScanGrayZygoteSpaceObjects"); |
| 815 | break; |
| 816 | case space::kGcRetentionPolicyAlwaysCollect: |
| 817 | timings_.StartSplit(paused ? "(Paused)ScanGrayAllocSpaceObjects" : |
| 818 | "ScanGrayAllocSpaceObjects"); |
| 819 | break; |
| 820 | } |
| 821 | ScanObjectVisitor visitor(this); |
| 822 | card_table->Scan(space->GetMarkBitmap(), space->Begin(), space->End(), visitor, minimum_age); |
| 823 | timings_.EndSplit(); |
| 824 | } |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 825 | } |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 826 | } |
| 827 | } |
| 828 | |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 829 | class RecursiveMarkTask : public MarkStackTask<false> { |
| 830 | public: |
| 831 | RecursiveMarkTask(ThreadPool* thread_pool, MarkSweep* mark_sweep, |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 832 | accounting::ContinuousSpaceBitmap* bitmap, uintptr_t begin, uintptr_t end) |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 833 | : MarkStackTask<false>(thread_pool, mark_sweep, 0, NULL), |
| 834 | bitmap_(bitmap), |
| 835 | begin_(begin), |
| 836 | end_(end) { |
| 837 | } |
| 838 | |
| 839 | protected: |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 840 | accounting::ContinuousSpaceBitmap* const bitmap_; |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 841 | const uintptr_t begin_; |
| 842 | const uintptr_t end_; |
| 843 | |
| 844 | virtual void Finalize() { |
| 845 | delete this; |
| 846 | } |
| 847 | |
| 848 | // Scans all of the objects |
| 849 | virtual void Run(Thread* self) NO_THREAD_SAFETY_ANALYSIS { |
| 850 | ScanObjectParallelVisitor visitor(this); |
| 851 | bitmap_->VisitMarkedRange(begin_, end_, visitor); |
| 852 | // Finish by emptying our local mark stack. |
| 853 | MarkStackTask::Run(self); |
| 854 | } |
| 855 | }; |
| 856 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 857 | // Populates the mark stack based on the set of marked objects and |
| 858 | // recursively marks until the mark stack is emptied. |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 859 | void MarkSweep::RecursiveMark() { |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 860 | TimingLogger::ScopedSplit split("RecursiveMark", &timings_); |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 861 | // RecursiveMark will build the lists of known instances of the Reference classes. See |
| 862 | // DelayReferenceReferent for details. |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 863 | if (kUseRecursiveMark) { |
| 864 | const bool partial = GetGcType() == kGcTypePartial; |
| 865 | ScanObjectVisitor scan_visitor(this); |
| 866 | auto* self = Thread::Current(); |
| 867 | ThreadPool* thread_pool = heap_->GetThreadPool(); |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 868 | size_t thread_count = GetThreadCount(false); |
| 869 | const bool parallel = kParallelRecursiveMark && thread_count > 1; |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 870 | mark_stack_->Reset(); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 871 | for (const auto& space : GetHeap()->GetContinuousSpaces()) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 872 | if ((space->GetGcRetentionPolicy() == space::kGcRetentionPolicyAlwaysCollect) || |
| 873 | (!partial && space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect)) { |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 874 | current_space_bitmap_ = space->GetMarkBitmap(); |
| 875 | if (current_space_bitmap_ == nullptr) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 876 | continue; |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 877 | } |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 878 | if (parallel) { |
| 879 | // We will use the mark stack the future. |
| 880 | // CHECK(mark_stack_->IsEmpty()); |
| 881 | // This function does not handle heap end increasing, so we must use the space end. |
| 882 | uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin()); |
| 883 | uintptr_t end = reinterpret_cast<uintptr_t>(space->End()); |
| 884 | atomic_finger_ = static_cast<int32_t>(0xFFFFFFFF); |
| 885 | |
| 886 | // Create a few worker tasks. |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 887 | const size_t n = thread_count * 2; |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 888 | while (begin != end) { |
| 889 | uintptr_t start = begin; |
| 890 | uintptr_t delta = (end - begin) / n; |
| 891 | delta = RoundUp(delta, KB); |
| 892 | if (delta < 16 * KB) delta = end - begin; |
| 893 | begin += delta; |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 894 | auto* task = new RecursiveMarkTask(thread_pool, this, current_space_bitmap_, start, |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 895 | begin); |
| 896 | thread_pool->AddTask(self, task); |
| 897 | } |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 898 | thread_pool->SetMaxActiveWorkers(thread_count - 1); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 899 | thread_pool->StartWorkers(self); |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 900 | thread_pool->Wait(self, true, true); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 901 | thread_pool->StopWorkers(self); |
| 902 | } else { |
| 903 | // This function does not handle heap end increasing, so we must use the space end. |
| 904 | uintptr_t begin = reinterpret_cast<uintptr_t>(space->Begin()); |
| 905 | uintptr_t end = reinterpret_cast<uintptr_t>(space->End()); |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 906 | current_space_bitmap_->VisitMarkedRange(begin, end, scan_visitor); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 907 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 908 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 909 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 910 | } |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 911 | ProcessMarkStack(false); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 912 | } |
| 913 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 914 | mirror::Object* MarkSweep::IsMarkedCallback(mirror::Object* object, void* arg) { |
Mathieu Chartier | 5712d5d | 2013-09-18 17:59:36 -0700 | [diff] [blame] | 915 | if (reinterpret_cast<MarkSweep*>(arg)->IsMarked(object)) { |
Mathieu Chartier | 6aa3df9 | 2013-09-17 15:17:28 -0700 | [diff] [blame] | 916 | return object; |
| 917 | } |
| 918 | return nullptr; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 919 | } |
| 920 | |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 921 | void MarkSweep::RecursiveMarkDirtyObjects(bool paused, byte minimum_age) { |
| 922 | ScanGrayObjects(paused, minimum_age); |
| 923 | ProcessMarkStack(paused); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 924 | } |
| 925 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 926 | void MarkSweep::ReMarkRoots() { |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 927 | Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current()); |
Mathieu Chartier | 3bb57c7 | 2014-02-18 11:38:45 -0800 | [diff] [blame] | 928 | timings_.StartSplit("(Paused)ReMarkRoots"); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 929 | Runtime::Current()->VisitRoots( |
| 930 | MarkRootCallback, this, static_cast<VisitRootFlags>(kVisitRootFlagNewRoots | |
| 931 | kVisitRootFlagStopLoggingNewRoots | |
| 932 | kVisitRootFlagClearRootLog)); |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 933 | timings_.EndSplit(); |
Mathieu Chartier | 7bf9f19 | 2014-04-04 11:09:41 -0700 | [diff] [blame] | 934 | if (kVerifyRootsMarked) { |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 935 | timings_.StartSplit("(Paused)VerifyRoots"); |
| 936 | Runtime::Current()->VisitRoots(VerifyRootMarked, this); |
| 937 | timings_.EndSplit(); |
| 938 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 939 | } |
| 940 | |
Mathieu Chartier | 0f7bf6a | 2014-03-28 10:05:39 -0700 | [diff] [blame] | 941 | void MarkSweep::SweepSystemWeaks(Thread* self) { |
| 942 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 943 | timings_.StartSplit("SweepSystemWeaks"); |
Mathieu Chartier | 0f7bf6a | 2014-03-28 10:05:39 -0700 | [diff] [blame] | 944 | Runtime::Current()->SweepSystemWeaks(IsMarkedCallback, this); |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 945 | timings_.EndSplit(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 946 | } |
| 947 | |
Mathieu Chartier | 6aa3df9 | 2013-09-17 15:17:28 -0700 | [diff] [blame] | 948 | mirror::Object* MarkSweep::VerifySystemWeakIsLiveCallback(Object* obj, void* arg) { |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 949 | reinterpret_cast<MarkSweep*>(arg)->VerifyIsLive(obj); |
| 950 | // 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] | 951 | return obj; |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | void MarkSweep::VerifyIsLive(const Object* obj) { |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 955 | if (!heap_->GetLiveBitmap()->Test(obj)) { |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 956 | if (std::find(heap_->allocation_stack_->Begin(), heap_->allocation_stack_->End(), obj) == |
| 957 | heap_->allocation_stack_->End()) { |
| 958 | // Object not found! |
| 959 | heap_->DumpSpaces(); |
| 960 | LOG(FATAL) << "Found dead object " << obj; |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 961 | } |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | void MarkSweep::VerifySystemWeaks() { |
Mathieu Chartier | 6aa3df9 | 2013-09-17 15:17:28 -0700 | [diff] [blame] | 966 | // Verify system weaks, uses a special object visitor which returns the input object. |
| 967 | Runtime::Current()->SweepSystemWeaks(VerifySystemWeakIsLiveCallback, this); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 968 | } |
| 969 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 970 | class CheckpointMarkThreadRoots : public Closure { |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 971 | public: |
Mathieu Chartier | 0f7bf6a | 2014-03-28 10:05:39 -0700 | [diff] [blame] | 972 | explicit CheckpointMarkThreadRoots(MarkSweep* mark_sweep, |
| 973 | bool revoke_ros_alloc_thread_local_buffers_at_checkpoint) |
| 974 | : mark_sweep_(mark_sweep), |
| 975 | revoke_ros_alloc_thread_local_buffers_at_checkpoint_( |
| 976 | revoke_ros_alloc_thread_local_buffers_at_checkpoint) { |
| 977 | } |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 978 | |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 979 | virtual void Run(Thread* thread) OVERRIDE NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | 3f96670 | 2013-09-04 16:50:05 -0700 | [diff] [blame] | 980 | ATRACE_BEGIN("Marking thread roots"); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 981 | // Note: self is not necessarily equal to thread since thread may be suspended. |
| 982 | Thread* self = Thread::Current(); |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 983 | CHECK(thread == self || thread->IsSuspended() || thread->GetState() == kWaitingPerformingGc) |
| 984 | << thread->GetState() << " thread " << thread << " self " << self; |
Mathieu Chartier | ac86a7c | 2012-11-12 15:03:16 -0800 | [diff] [blame] | 985 | thread->VisitRoots(MarkSweep::MarkRootParallelCallback, mark_sweep_); |
Mathieu Chartier | 3f96670 | 2013-09-04 16:50:05 -0700 | [diff] [blame] | 986 | ATRACE_END(); |
Mathieu Chartier | 0f7bf6a | 2014-03-28 10:05:39 -0700 | [diff] [blame] | 987 | if (revoke_ros_alloc_thread_local_buffers_at_checkpoint_) { |
| 988 | ATRACE_BEGIN("RevokeRosAllocThreadLocalBuffers"); |
Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 989 | mark_sweep_->GetHeap()->RevokeRosAllocThreadLocalBuffers(thread); |
Mathieu Chartier | 0f7bf6a | 2014-03-28 10:05:39 -0700 | [diff] [blame] | 990 | ATRACE_END(); |
Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 991 | } |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 992 | mark_sweep_->GetBarrier().Pass(self); |
| 993 | } |
| 994 | |
| 995 | private: |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 996 | MarkSweep* const mark_sweep_; |
Mathieu Chartier | 0f7bf6a | 2014-03-28 10:05:39 -0700 | [diff] [blame] | 997 | const bool revoke_ros_alloc_thread_local_buffers_at_checkpoint_; |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 998 | }; |
| 999 | |
Mathieu Chartier | 0f7bf6a | 2014-03-28 10:05:39 -0700 | [diff] [blame] | 1000 | void MarkSweep::MarkRootsCheckpoint(Thread* self, |
| 1001 | bool revoke_ros_alloc_thread_local_buffers_at_checkpoint) { |
| 1002 | CheckpointMarkThreadRoots check_point(this, revoke_ros_alloc_thread_local_buffers_at_checkpoint); |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 1003 | timings_.StartSplit("MarkRootsCheckpoint"); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 1004 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1005 | // Request the check point is run on all threads returning a count of the threads that must |
| 1006 | // run through the barrier including self. |
| 1007 | size_t barrier_count = thread_list->RunCheckpoint(&check_point); |
| 1008 | // Release locks then wait for all mutator threads to pass the barrier. |
| 1009 | // TODO: optimize to not release locks when there are no threads to wait for. |
| 1010 | Locks::heap_bitmap_lock_->ExclusiveUnlock(self); |
| 1011 | Locks::mutator_lock_->SharedUnlock(self); |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 1012 | { |
| 1013 | ScopedThreadStateChange tsc(self, kWaitingForCheckPointsToRun); |
| 1014 | gc_barrier_->Increment(self, barrier_count); |
| 1015 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1016 | Locks::mutator_lock_->SharedLock(self); |
| 1017 | Locks::heap_bitmap_lock_->ExclusiveLock(self); |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 1018 | timings_.EndSplit(); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 1019 | } |
| 1020 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1021 | void MarkSweep::SweepArray(accounting::ObjectStack* allocations, bool swap_bitmaps) { |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 1022 | timings_.StartSplit("SweepArray"); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 1023 | Thread* self = Thread::Current(); |
| 1024 | mirror::Object* chunk_free_buffer[kSweepArrayChunkFreeSize]; |
| 1025 | size_t chunk_free_pos = 0; |
Mathieu Chartier | e53225c | 2013-08-19 10:59:11 -0700 | [diff] [blame] | 1026 | size_t freed_bytes = 0; |
| 1027 | size_t freed_large_object_bytes = 0; |
Hiroshi Yamauchi | b22a451 | 2013-08-13 15:03:22 -0700 | [diff] [blame] | 1028 | size_t freed_objects = 0; |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1029 | size_t freed_large_objects = 0; |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 1030 | // How many objects are left in the array, modified after each space is swept. |
Mathieu Chartier | 0f7bf6a | 2014-03-28 10:05:39 -0700 | [diff] [blame] | 1031 | Object** objects = allocations->Begin(); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 1032 | size_t count = allocations->Size(); |
| 1033 | // Change the order to ensure that the non-moving space last swept as an optimization. |
| 1034 | std::vector<space::ContinuousSpace*> sweep_spaces; |
| 1035 | space::ContinuousSpace* non_moving_space = nullptr; |
| 1036 | for (space::ContinuousSpace* space : heap_->GetContinuousSpaces()) { |
Mathieu Chartier | 8d56210 | 2014-03-12 17:42:10 -0700 | [diff] [blame] | 1037 | if (space->IsAllocSpace() && !immune_region_.ContainsSpace(space) && |
| 1038 | space->GetLiveBitmap() != nullptr) { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 1039 | if (space == heap_->GetNonMovingSpace()) { |
| 1040 | non_moving_space = space; |
| 1041 | } else { |
| 1042 | sweep_spaces.push_back(space); |
| 1043 | } |
| 1044 | } |
| 1045 | } |
| 1046 | // Unlikely to sweep a significant amount of non_movable objects, so we do these after the after |
| 1047 | // the other alloc spaces as an optimization. |
| 1048 | if (non_moving_space != nullptr) { |
| 1049 | sweep_spaces.push_back(non_moving_space); |
| 1050 | } |
| 1051 | // Start by sweeping the continuous spaces. |
| 1052 | for (space::ContinuousSpace* space : sweep_spaces) { |
| 1053 | space::AllocSpace* alloc_space = space->AsAllocSpace(); |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 1054 | accounting::ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
| 1055 | accounting::ContinuousSpaceBitmap* mark_bitmap = space->GetMarkBitmap(); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 1056 | if (swap_bitmaps) { |
| 1057 | std::swap(live_bitmap, mark_bitmap); |
| 1058 | } |
| 1059 | Object** out = objects; |
| 1060 | for (size_t i = 0; i < count; ++i) { |
| 1061 | Object* obj = objects[i]; |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 1062 | if (kUseThreadLocalAllocationStack && obj == nullptr) { |
| 1063 | continue; |
| 1064 | } |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 1065 | if (space->HasAddress(obj)) { |
| 1066 | // This object is in the space, remove it from the array and add it to the sweep buffer |
| 1067 | // if needed. |
| 1068 | if (!mark_bitmap->Test(obj)) { |
| 1069 | if (chunk_free_pos >= kSweepArrayChunkFreeSize) { |
| 1070 | timings_.StartSplit("FreeList"); |
| 1071 | freed_objects += chunk_free_pos; |
| 1072 | freed_bytes += alloc_space->FreeList(self, chunk_free_pos, chunk_free_buffer); |
| 1073 | timings_.EndSplit(); |
| 1074 | chunk_free_pos = 0; |
| 1075 | } |
| 1076 | chunk_free_buffer[chunk_free_pos++] = obj; |
| 1077 | } |
| 1078 | } else { |
| 1079 | *(out++) = obj; |
| 1080 | } |
| 1081 | } |
| 1082 | if (chunk_free_pos > 0) { |
| 1083 | timings_.StartSplit("FreeList"); |
| 1084 | freed_objects += chunk_free_pos; |
| 1085 | freed_bytes += alloc_space->FreeList(self, chunk_free_pos, chunk_free_buffer); |
| 1086 | timings_.EndSplit(); |
| 1087 | chunk_free_pos = 0; |
| 1088 | } |
| 1089 | // All of the references which space contained are no longer in the allocation stack, update |
| 1090 | // the count. |
| 1091 | count = out - objects; |
| 1092 | } |
| 1093 | // Handle the large object space. |
| 1094 | space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 1095 | accounting::LargeObjectBitmap* large_live_objects = large_object_space->GetLiveBitmap(); |
| 1096 | accounting::LargeObjectBitmap* large_mark_objects = large_object_space->GetMarkBitmap(); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 1097 | if (swap_bitmaps) { |
| 1098 | std::swap(large_live_objects, large_mark_objects); |
| 1099 | } |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame] | 1100 | for (size_t i = 0; i < count; ++i) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1101 | Object* obj = objects[i]; |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 1102 | // Handle large objects. |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 1103 | if (kUseThreadLocalAllocationStack && obj == nullptr) { |
| 1104 | continue; |
| 1105 | } |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 1106 | if (!large_mark_objects->Test(obj)) { |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1107 | ++freed_large_objects; |
Mathieu Chartier | e53225c | 2013-08-19 10:59:11 -0700 | [diff] [blame] | 1108 | freed_large_object_bytes += large_object_space->Free(self, obj); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1109 | } |
| 1110 | } |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 1111 | timings_.EndSplit(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1112 | |
Hiroshi Yamauchi | b22a451 | 2013-08-13 15:03:22 -0700 | [diff] [blame] | 1113 | timings_.StartSplit("RecordFree"); |
Mathieu Chartier | e76e70f | 2014-05-02 16:35:37 -0700 | [diff] [blame] | 1114 | VLOG(heap) << "Freed " << freed_objects << "/" << count << " objects with size " |
| 1115 | << PrettySize(freed_bytes); |
| 1116 | RecordFree(freed_objects, freed_bytes); |
| 1117 | RecordFreeLargeObjects(freed_large_objects, freed_large_object_bytes); |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 1118 | timings_.EndSplit(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1119 | |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 1120 | timings_.StartSplit("ResetStack"); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1121 | allocations->Reset(); |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 1122 | timings_.EndSplit(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1123 | } |
| 1124 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1125 | void MarkSweep::Sweep(bool swap_bitmaps) { |
Mathieu Chartier | 0f7bf6a | 2014-03-28 10:05:39 -0700 | [diff] [blame] | 1126 | // Ensure that nobody inserted items in the live stack after we swapped the stacks. |
| 1127 | CHECK_GE(live_stack_freeze_size_, GetHeap()->GetLiveStack()->Size()); |
| 1128 | // Mark everything allocated since the last as GC live so that we can sweep concurrently, |
| 1129 | // knowing that new allocations won't be marked as live. |
| 1130 | timings_.StartSplit("MarkStackAsLive"); |
| 1131 | accounting::ObjectStack* live_stack = heap_->GetLiveStack(); |
| 1132 | heap_->MarkAllocStackAsLive(live_stack); |
| 1133 | live_stack->Reset(); |
| 1134 | timings_.EndSplit(); |
| 1135 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 1136 | DCHECK(mark_stack_->IsEmpty()); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1137 | for (const auto& space : GetHeap()->GetContinuousSpaces()) { |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 1138 | if (space->IsContinuousMemMapAllocSpace()) { |
| 1139 | space::ContinuousMemMapAllocSpace* alloc_space = space->AsContinuousMemMapAllocSpace(); |
Mathieu Chartier | ec05007 | 2014-01-07 16:00:07 -0800 | [diff] [blame] | 1140 | TimingLogger::ScopedSplit split( |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 1141 | alloc_space->IsZygoteSpace() ? "SweepZygoteSpace" : "SweepMallocSpace", &timings_); |
Mathieu Chartier | ec05007 | 2014-01-07 16:00:07 -0800 | [diff] [blame] | 1142 | size_t freed_objects = 0; |
| 1143 | size_t freed_bytes = 0; |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 1144 | alloc_space->Sweep(swap_bitmaps, &freed_objects, &freed_bytes); |
Mathieu Chartier | e76e70f | 2014-05-02 16:35:37 -0700 | [diff] [blame] | 1145 | RecordFree(freed_objects, freed_bytes); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 1146 | } |
| 1147 | } |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1148 | SweepLargeObjects(swap_bitmaps); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 1149 | } |
| 1150 | |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1151 | void MarkSweep::SweepLargeObjects(bool swap_bitmaps) { |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 1152 | TimingLogger::ScopedSplit split("SweepLargeObjects", &timings_); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1153 | size_t freed_objects = 0; |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 1154 | size_t freed_bytes = 0; |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 1155 | heap_->GetLargeObjectsSpace()->Sweep(swap_bitmaps, &freed_objects, &freed_bytes); |
Mathieu Chartier | e76e70f | 2014-05-02 16:35:37 -0700 | [diff] [blame] | 1156 | RecordFreeLargeObjects(freed_objects, freed_bytes); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1157 | } |
| 1158 | |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 1159 | // Process the "referent" field in a java.lang.ref.Reference. If the referent has not yet been |
| 1160 | // marked, put it on the appropriate list in the heap for later processing. |
| 1161 | void MarkSweep::DelayReferenceReferent(mirror::Class* klass, mirror::Reference* ref) { |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 1162 | DCHECK(klass != nullptr); |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 1163 | if (kCountJavaLangRefs) { |
| 1164 | ++reference_count_; |
| 1165 | } |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame^] | 1166 | heap_->GetReferenceProcessor()->DelayReferenceReferent(klass, ref, IsMarkedCallback, this); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1167 | } |
| 1168 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1169 | class MarkObjectVisitor { |
| 1170 | public: |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 1171 | explicit MarkObjectVisitor(MarkSweep* const mark_sweep) ALWAYS_INLINE : mark_sweep_(mark_sweep) { |
| 1172 | } |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1173 | |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 1174 | void operator()(Object* obj, MemberOffset offset, bool /* is_static */) const |
| 1175 | ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 1176 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 1177 | if (kCheckLocks) { |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1178 | Locks::mutator_lock_->AssertSharedHeld(Thread::Current()); |
| 1179 | Locks::heap_bitmap_lock_->AssertExclusiveHeld(Thread::Current()); |
| 1180 | } |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 1181 | mark_sweep_->MarkObject(obj->GetFieldObject<mirror::Object>(offset)); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | private: |
| 1185 | MarkSweep* const mark_sweep_; |
| 1186 | }; |
| 1187 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1188 | // Scans an object reference. Determines the type of the reference |
| 1189 | // and dispatches to a specialized scanning routine. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1190 | void MarkSweep::ScanObject(Object* obj) { |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 1191 | MarkObjectVisitor mark_visitor(this); |
| 1192 | DelayReferenceReferentVisitor ref_visitor(this); |
| 1193 | ScanObjectVisit(obj, mark_visitor, ref_visitor); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1194 | } |
| 1195 | |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame^] | 1196 | void MarkSweep::ProcessMarkStackCallback(void* arg) { |
| 1197 | reinterpret_cast<MarkSweep*>(arg)->ProcessMarkStack(false); |
Mathieu Chartier | 3bb57c7 | 2014-02-18 11:38:45 -0800 | [diff] [blame] | 1198 | } |
| 1199 | |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 1200 | void MarkSweep::ProcessMarkStackParallel(size_t thread_count) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1201 | Thread* self = Thread::Current(); |
| 1202 | ThreadPool* thread_pool = GetHeap()->GetThreadPool(); |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 1203 | const size_t chunk_size = std::min(mark_stack_->Size() / thread_count + 1, |
| 1204 | static_cast<size_t>(MarkStackTask<false>::kMaxSize)); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 1205 | CHECK_GT(chunk_size, 0U); |
| 1206 | // Split the current mark stack up into work tasks. |
| 1207 | for (mirror::Object **it = mark_stack_->Begin(), **end = mark_stack_->End(); it < end; ) { |
| 1208 | const size_t delta = std::min(static_cast<size_t>(end - it), chunk_size); |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 1209 | thread_pool->AddTask(self, new MarkStackTask<false>(thread_pool, this, delta, it)); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 1210 | it += delta; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1211 | } |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 1212 | thread_pool->SetMaxActiveWorkers(thread_count - 1); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1213 | thread_pool->StartWorkers(self); |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 1214 | thread_pool->Wait(self, true, true); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 1215 | thread_pool->StopWorkers(self); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1216 | mark_stack_->Reset(); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1217 | 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] | 1218 | } |
| 1219 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 1220 | // Scan anything that's on the mark stack. |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 1221 | void MarkSweep::ProcessMarkStack(bool paused) { |
Mathieu Chartier | 3bb57c7 | 2014-02-18 11:38:45 -0800 | [diff] [blame] | 1222 | timings_.StartSplit(paused ? "(Paused)ProcessMarkStack" : "ProcessMarkStack"); |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 1223 | size_t thread_count = GetThreadCount(paused); |
| 1224 | if (kParallelProcessMarkStack && thread_count > 1 && |
| 1225 | mark_stack_->Size() >= kMinimumParallelMarkStackSize) { |
| 1226 | ProcessMarkStackParallel(thread_count); |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 1227 | } else { |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 1228 | // TODO: Tune this. |
| 1229 | static const size_t kFifoSize = 4; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1230 | BoundedFifoPowerOfTwo<Object*, kFifoSize> prefetch_fifo; |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 1231 | for (;;) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1232 | Object* obj = NULL; |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 1233 | if (kUseMarkStackPrefetch) { |
| 1234 | while (!mark_stack_->IsEmpty() && prefetch_fifo.size() < kFifoSize) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1235 | Object* obj = mark_stack_->PopBack(); |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 1236 | DCHECK(obj != NULL); |
| 1237 | __builtin_prefetch(obj); |
| 1238 | prefetch_fifo.push_back(obj); |
| 1239 | } |
| 1240 | if (prefetch_fifo.empty()) { |
| 1241 | break; |
| 1242 | } |
| 1243 | obj = prefetch_fifo.front(); |
| 1244 | prefetch_fifo.pop_front(); |
| 1245 | } else { |
| 1246 | if (mark_stack_->IsEmpty()) { |
| 1247 | break; |
| 1248 | } |
| 1249 | obj = mark_stack_->PopBack(); |
| 1250 | } |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 1251 | DCHECK(obj != nullptr); |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 1252 | ScanObject(obj); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1253 | } |
| 1254 | } |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 1255 | timings_.EndSplit(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1256 | } |
| 1257 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1258 | inline bool MarkSweep::IsMarked(const Object* object) const |
| 1259 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
Mathieu Chartier | 8d56210 | 2014-03-12 17:42:10 -0700 | [diff] [blame] | 1260 | if (immune_region_.ContainsObject(object)) { |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1261 | return true; |
| 1262 | } |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 1263 | if (current_space_bitmap_->HasAddress(object)) { |
| 1264 | return current_space_bitmap_->Test(object); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1265 | } |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 1266 | return mark_bitmap_->Test(object); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1267 | } |
| 1268 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1269 | void MarkSweep::FinishPhase() { |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 1270 | TimingLogger::ScopedSplit split("FinishPhase", &timings_); |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1271 | if (kCountScannedTypes) { |
| 1272 | VLOG(gc) << "MarkSweep scanned classes=" << class_count_ << " arrays=" << array_count_ |
| 1273 | << " other=" << other_count_; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1274 | } |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1275 | if (kCountTasks) { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1276 | VLOG(gc) << "Total number of work chunks allocated: " << work_chunks_created_; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1277 | } |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1278 | if (kMeasureOverhead) { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1279 | VLOG(gc) << "Overhead time " << PrettyDuration(overhead_time_); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1280 | } |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1281 | if (kProfileLargeObjects) { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1282 | VLOG(gc) << "Large objects tested " << large_object_test_ << " marked " << large_object_mark_; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1283 | } |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1284 | if (kCountJavaLangRefs) { |
| 1285 | VLOG(gc) << "References scanned " << reference_count_; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1286 | } |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 1287 | if (kCountMarkedObjects) { |
| 1288 | VLOG(gc) << "Marked: null=" << mark_null_count_ << " immune=" << mark_immune_count_ |
| 1289 | << " fastpath=" << mark_fastpath_count_ << " slowpath=" << mark_slowpath_count_; |
| 1290 | } |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 1291 | CHECK(mark_stack_->IsEmpty()); // Ensure that the mark stack is empty. |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 1292 | mark_stack_->Reset(); |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 1293 | WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 1294 | heap_->ClearMarkedObjects(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1295 | } |
| 1296 | |
Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 1297 | void MarkSweep::RevokeAllThreadLocalBuffers() { |
| 1298 | if (kRevokeRosAllocThreadLocalBuffersAtCheckpoint && IsConcurrent()) { |
| 1299 | // If concurrent, rosalloc thread-local buffers are revoked at the |
| 1300 | // thread checkpoint. Bump pointer space thread-local buffers must |
| 1301 | // not be in use. |
| 1302 | GetHeap()->AssertAllBumpPointerSpaceThreadLocalBuffersAreRevoked(); |
| 1303 | } else { |
| 1304 | timings_.StartSplit("(Paused)RevokeAllThreadLocalBuffers"); |
| 1305 | GetHeap()->RevokeAllThreadLocalBuffers(); |
| 1306 | timings_.EndSplit(); |
| 1307 | } |
| 1308 | } |
| 1309 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1310 | } // namespace collector |
| 1311 | } // namespace gc |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1312 | } // namespace art |