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