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