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