Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | */ |
| 16 | |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 17 | #include "semi_space-inl.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 18 | |
| 19 | #include <functional> |
| 20 | #include <numeric> |
| 21 | #include <climits> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include "base/logging.h" |
| 25 | #include "base/macros.h" |
| 26 | #include "base/mutex-inl.h" |
| 27 | #include "base/timing_logger.h" |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 28 | #include "gc/accounting/heap_bitmap-inl.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 29 | #include "gc/accounting/mod_union_table.h" |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 30 | #include "gc/accounting/remembered_set.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 31 | #include "gc/accounting/space_bitmap-inl.h" |
| 32 | #include "gc/heap.h" |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 33 | #include "gc/reference_processor.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 34 | #include "gc/space/bump_pointer_space.h" |
| 35 | #include "gc/space/bump_pointer_space-inl.h" |
| 36 | #include "gc/space/image_space.h" |
| 37 | #include "gc/space/large_object_space.h" |
| 38 | #include "gc/space/space-inl.h" |
| 39 | #include "indirect_reference_table.h" |
| 40 | #include "intern_table.h" |
| 41 | #include "jni_internal.h" |
| 42 | #include "mark_sweep-inl.h" |
| 43 | #include "monitor.h" |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 44 | #include "mirror/reference-inl.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 45 | #include "mirror/object-inl.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 46 | #include "runtime.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 47 | #include "thread-inl.h" |
| 48 | #include "thread_list.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 49 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 50 | using ::art::mirror::Object; |
| 51 | |
| 52 | namespace art { |
| 53 | namespace gc { |
| 54 | namespace collector { |
| 55 | |
| 56 | static constexpr bool kProtectFromSpace = true; |
Mathieu Chartier | 15d3402 | 2014-02-26 17:16:38 -0800 | [diff] [blame] | 57 | static constexpr bool kStoreStackTraces = false; |
Hiroshi Yamauchi | df386c5 | 2014-04-08 16:21:52 -0700 | [diff] [blame] | 58 | static constexpr size_t kBytesPromotedThreshold = 4 * MB; |
Hiroshi Yamauchi | 24faeb2 | 2014-05-07 13:12:43 -0700 | [diff] [blame] | 59 | static constexpr size_t kLargeObjectBytesAllocatedThreshold = 16 * MB; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 60 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 61 | void SemiSpace::BindBitmaps() { |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 62 | GetTimings()->StartSplit("BindBitmaps"); |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 63 | WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 64 | // Mark all of the spaces we never collect as immune. |
| 65 | for (const auto& space : GetHeap()->GetContinuousSpaces()) { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 66 | if (space->GetLiveBitmap() != nullptr) { |
| 67 | if (space == to_space_) { |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 68 | CHECK(to_space_->IsContinuousMemMapAllocSpace()); |
| 69 | to_space_->AsContinuousMemMapAllocSpace()->BindLiveToMarkBitmap(); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 70 | } else if (space->GetGcRetentionPolicy() == space::kGcRetentionPolicyNeverCollect |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 71 | || space->GetGcRetentionPolicy() == space::kGcRetentionPolicyFullCollect |
| 72 | // Add the main free list space and the non-moving |
| 73 | // space to the immune space if a bump pointer space |
| 74 | // only collection. |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 75 | || (generational_ && !whole_heap_collection_ && |
| 76 | (space == GetHeap()->GetNonMovingSpace() || |
| 77 | space == GetHeap()->GetPrimaryFreeListSpace()))) { |
Mathieu Chartier | 8d56210 | 2014-03-12 17:42:10 -0700 | [diff] [blame] | 78 | CHECK(immune_region_.AddContinuousSpace(space)) << "Failed to add space " << *space; |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 79 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 80 | } |
| 81 | } |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 82 | if (generational_ && !whole_heap_collection_) { |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 83 | // We won't collect the large object space if a bump pointer space only collection. |
| 84 | is_large_object_space_immune_ = true; |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 85 | } |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 86 | GetTimings()->EndSplit(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 89 | SemiSpace::SemiSpace(Heap* heap, bool generational, const std::string& name_prefix) |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 90 | : GarbageCollector(heap, |
| 91 | name_prefix + (name_prefix.empty() ? "" : " ") + "marksweep + semispace"), |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 92 | to_space_(nullptr), |
| 93 | from_space_(nullptr), |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 94 | generational_(generational), |
Hiroshi Yamauchi | 4b1782f | 2013-12-05 16:46:22 -0800 | [diff] [blame] | 95 | last_gc_to_space_end_(nullptr), |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 96 | bytes_promoted_(0), |
Hiroshi Yamauchi | df386c5 | 2014-04-08 16:21:52 -0700 | [diff] [blame] | 97 | bytes_promoted_since_last_whole_heap_collection_(0), |
Hiroshi Yamauchi | 24faeb2 | 2014-05-07 13:12:43 -0700 | [diff] [blame] | 98 | large_object_bytes_allocated_at_last_whole_heap_collection_(0), |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 99 | whole_heap_collection_(true), |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 100 | collector_name_(name_), |
| 101 | swap_semi_spaces_(true) { |
| 102 | } |
| 103 | |
| 104 | void SemiSpace::RunPhases() { |
| 105 | Thread* self = Thread::Current(); |
| 106 | InitializePhase(); |
| 107 | // Semi-space collector is special since it is sometimes called with the mutators suspended |
| 108 | // during the zygote creation and collector transitions. If we already exclusively hold the |
| 109 | // mutator lock, then we can't lock it again since it will cause a deadlock. |
| 110 | if (Locks::mutator_lock_->IsExclusiveHeld(self)) { |
| 111 | GetHeap()->PreGcVerificationPaused(this); |
| 112 | GetHeap()->PrePauseRosAllocVerification(this); |
| 113 | MarkingPhase(); |
| 114 | ReclaimPhase(); |
| 115 | GetHeap()->PostGcVerificationPaused(this); |
| 116 | } else { |
| 117 | Locks::mutator_lock_->AssertNotHeld(self); |
| 118 | { |
| 119 | ScopedPause pause(this); |
| 120 | GetHeap()->PreGcVerificationPaused(this); |
| 121 | GetHeap()->PrePauseRosAllocVerification(this); |
| 122 | MarkingPhase(); |
| 123 | } |
| 124 | { |
| 125 | ReaderMutexLock mu(self, *Locks::mutator_lock_); |
| 126 | ReclaimPhase(); |
| 127 | } |
| 128 | GetHeap()->PostGcVerification(this); |
| 129 | } |
| 130 | FinishPhase(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void SemiSpace::InitializePhase() { |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 134 | TimingLogger::ScopedSplit split("InitializePhase", GetTimings()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 135 | mark_stack_ = heap_->GetMarkStack(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 136 | DCHECK(mark_stack_ != nullptr); |
Mathieu Chartier | 8d56210 | 2014-03-12 17:42:10 -0700 | [diff] [blame] | 137 | immune_region_.Reset(); |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 138 | is_large_object_space_immune_ = false; |
Mathieu Chartier | ad35d90 | 2014-02-11 16:20:42 -0800 | [diff] [blame] | 139 | saved_bytes_ = 0; |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 140 | bytes_moved_ = 0; |
| 141 | objects_moved_ = 0; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 142 | self_ = Thread::Current(); |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 143 | CHECK(from_space_->CanMoveObjects()) << "Attempting to move from " << *from_space_; |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 144 | // Set the initial bitmap. |
| 145 | to_space_live_bitmap_ = to_space_->GetLiveBitmap(); |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 146 | { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 147 | // TODO: I don't think we should need heap bitmap lock to Get the mark bitmap. |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 148 | ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 149 | mark_bitmap_ = heap_->GetMarkBitmap(); |
| 150 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void SemiSpace::ProcessReferences(Thread* self) { |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 154 | TimingLogger::ScopedSplit split("ProcessReferences", GetTimings()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 155 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 156 | GetHeap()->GetReferenceProcessor()->ProcessReferences( |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 157 | false, GetTimings(), GetCurrentIteration()->GetClearSoftReferences(), |
| 158 | &HeapReferenceMarkedCallback, &MarkObjectCallback, &ProcessMarkStackCallback, this); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void SemiSpace::MarkingPhase() { |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 162 | CHECK(Locks::mutator_lock_->IsExclusiveHeld(self_)); |
Mathieu Chartier | 15d3402 | 2014-02-26 17:16:38 -0800 | [diff] [blame] | 163 | if (kStoreStackTraces) { |
| 164 | Locks::mutator_lock_->AssertExclusiveHeld(self_); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 165 | // Store the stack traces into the runtime fault string in case we Get a heap corruption |
Mathieu Chartier | 15d3402 | 2014-02-26 17:16:38 -0800 | [diff] [blame] | 166 | // related crash later. |
| 167 | ThreadState old_state = self_->SetStateUnsafe(kRunnable); |
| 168 | std::ostringstream oss; |
| 169 | Runtime* runtime = Runtime::Current(); |
| 170 | runtime->GetThreadList()->DumpForSigQuit(oss); |
| 171 | runtime->GetThreadList()->DumpNativeStacks(oss); |
| 172 | runtime->SetFaultMessage(oss.str()); |
| 173 | CHECK_EQ(self_->SetStateUnsafe(old_state), kRunnable); |
| 174 | } |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 175 | // Revoke the thread local buffers since the GC may allocate into a RosAllocSpace and this helps |
| 176 | // to prevent fragmentation. |
| 177 | RevokeAllThreadLocalBuffers(); |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 178 | if (generational_) { |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 179 | if (GetCurrentIteration()->GetGcCause() == kGcCauseExplicit || |
| 180 | GetCurrentIteration()->GetGcCause() == kGcCauseForNativeAlloc || |
| 181 | GetCurrentIteration()->GetClearSoftReferences()) { |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 182 | // If an explicit, native allocation-triggered, or last attempt |
Hiroshi Yamauchi | 24faeb2 | 2014-05-07 13:12:43 -0700 | [diff] [blame] | 183 | // collection, collect the whole heap. |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 184 | whole_heap_collection_ = true; |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 185 | } |
| 186 | if (whole_heap_collection_) { |
| 187 | VLOG(heap) << "Whole heap collection"; |
Hiroshi Yamauchi | df386c5 | 2014-04-08 16:21:52 -0700 | [diff] [blame] | 188 | name_ = collector_name_ + " whole"; |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 189 | } else { |
| 190 | VLOG(heap) << "Bump pointer space only collection"; |
Hiroshi Yamauchi | df386c5 | 2014-04-08 16:21:52 -0700 | [diff] [blame] | 191 | name_ = collector_name_ + " bps"; |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 192 | } |
| 193 | } |
Hiroshi Yamauchi | df386c5 | 2014-04-08 16:21:52 -0700 | [diff] [blame] | 194 | |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 195 | if (!generational_ || whole_heap_collection_) { |
| 196 | // If non-generational, always clear soft references. |
| 197 | // If generational, clear soft references if a whole heap collection. |
| 198 | GetCurrentIteration()->SetClearSoftReferences(true); |
Hiroshi Yamauchi | df386c5 | 2014-04-08 16:21:52 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 201 | Locks::mutator_lock_->AssertExclusiveHeld(self_); |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 202 | |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 203 | TimingLogger::ScopedSplit split("MarkingPhase", GetTimings()); |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 204 | if (generational_) { |
Hiroshi Yamauchi | 4b1782f | 2013-12-05 16:46:22 -0800 | [diff] [blame] | 205 | // If last_gc_to_space_end_ is out of the bounds of the from-space |
| 206 | // (the to-space from last GC), then point it to the beginning of |
| 207 | // the from-space. For example, the very first GC or the |
| 208 | // pre-zygote compaction. |
| 209 | if (!from_space_->HasAddress(reinterpret_cast<mirror::Object*>(last_gc_to_space_end_))) { |
| 210 | last_gc_to_space_end_ = from_space_->Begin(); |
| 211 | } |
| 212 | // Reset this before the marking starts below. |
| 213 | bytes_promoted_ = 0; |
| 214 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 215 | // Assume the cleared space is already empty. |
| 216 | BindBitmaps(); |
| 217 | // Process dirty cards and add dirty cards to mod-union tables. |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 218 | heap_->ProcessCards(GetTimings(), kUseRememberedSet && generational_); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 219 | // Clear the whole card table since we can not Get any additional dirty cards during the |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 220 | // paused GC. This saves memory but only works for pause the world collectors. |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 221 | GetTimings()->NewSplit("ClearCardTable"); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 222 | heap_->GetCardTable()->ClearCardTable(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 223 | // Need to do this before the checkpoint since we don't want any threads to add references to |
| 224 | // the live stack during the recursive mark. |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 225 | GetTimings()->NewSplit("SwapStacks"); |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 226 | if (kUseThreadLocalAllocationStack) { |
| 227 | heap_->RevokeAllThreadLocalAllocationStacks(self_); |
| 228 | } |
| 229 | heap_->SwapStacks(self_); |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 230 | { |
| 231 | WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_); |
| 232 | MarkRoots(); |
| 233 | // Mark roots of immune spaces. |
| 234 | UpdateAndMarkModUnion(); |
| 235 | // Recursively mark remaining objects. |
| 236 | MarkReachableObjects(); |
| 237 | } |
| 238 | ProcessReferences(self_); |
| 239 | { |
| 240 | ReaderMutexLock mu(self_, *Locks::heap_bitmap_lock_); |
| 241 | SweepSystemWeaks(); |
| 242 | } |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 243 | GetTimings()->NewSplit("RecordFree"); |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 244 | // Revoke buffers before measuring how many objects were moved since the TLABs need to be revoked |
| 245 | // before they are properly counted. |
| 246 | RevokeAllThreadLocalBuffers(); |
| 247 | // Record freed memory. |
Mathieu Chartier | e76e70f | 2014-05-02 16:35:37 -0700 | [diff] [blame] | 248 | const int64_t from_bytes = from_space_->GetBytesAllocated(); |
| 249 | const int64_t to_bytes = bytes_moved_; |
| 250 | const uint64_t from_objects = from_space_->GetObjectsAllocated(); |
| 251 | const uint64_t to_objects = objects_moved_; |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 252 | CHECK_LE(to_objects, from_objects); |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 253 | // Note: Freed bytes can be negative if we copy form a compacted space to a free-list backed |
| 254 | // space. |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 255 | RecordFree(ObjectBytePair(from_objects - to_objects, from_bytes - to_bytes)); |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 256 | // Clear and protect the from space. |
| 257 | from_space_->Clear(); |
Mathieu Chartier | e76e70f | 2014-05-02 16:35:37 -0700 | [diff] [blame] | 258 | VLOG(heap) << "Protecting from_space_: " << *from_space_; |
| 259 | from_space_->GetMemMap()->Protect(kProtectFromSpace ? PROT_NONE : PROT_READ); |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 260 | GetTimings()->StartSplit("PreSweepingGcVerification"); |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 261 | heap_->PreSweepingGcVerification(this); |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 262 | GetTimings()->EndSplit(); |
Mathieu Chartier | 4240c51 | 2014-05-27 10:10:11 -0700 | [diff] [blame] | 263 | if (swap_semi_spaces_) { |
| 264 | heap_->SwapSemiSpaces(); |
| 265 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 268 | void SemiSpace::UpdateAndMarkModUnion() { |
| 269 | for (auto& space : heap_->GetContinuousSpaces()) { |
| 270 | // If the space is immune then we need to mark the references to other spaces. |
Mathieu Chartier | 8d56210 | 2014-03-12 17:42:10 -0700 | [diff] [blame] | 271 | if (immune_region_.ContainsSpace(space)) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 272 | accounting::ModUnionTable* table = heap_->FindModUnionTableFromSpace(space); |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 273 | if (table != nullptr) { |
| 274 | // TODO: Improve naming. |
| 275 | TimingLogger::ScopedSplit split( |
| 276 | space->IsZygoteSpace() ? "UpdateAndMarkZygoteModUnionTable" : |
| 277 | "UpdateAndMarkImageModUnionTable", |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 278 | GetTimings()); |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 279 | table->UpdateAndMarkReferences(MarkHeapReferenceCallback, this); |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 280 | } else if (heap_->FindRememberedSetFromSpace(space) != nullptr) { |
| 281 | DCHECK(kUseRememberedSet); |
| 282 | // If a bump pointer space only collection, the non-moving |
| 283 | // space is added to the immune space. The non-moving space |
| 284 | // doesn't have a mod union table, but has a remembered |
| 285 | // set. Its dirty cards will be scanned later in |
| 286 | // MarkReachableObjects(). |
| 287 | DCHECK(generational_ && !whole_heap_collection_ && |
| 288 | (space == heap_->GetNonMovingSpace() || space == heap_->GetPrimaryFreeListSpace())) |
| 289 | << "Space " << space->GetName() << " " |
| 290 | << "generational_=" << generational_ << " " |
| 291 | << "whole_heap_collection_=" << whole_heap_collection_ << " "; |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 292 | } else { |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 293 | DCHECK(!kUseRememberedSet); |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 294 | // If a bump pointer space only collection, the non-moving |
| 295 | // space is added to the immune space. But the non-moving |
| 296 | // space doesn't have a mod union table. Instead, its live |
| 297 | // bitmap will be scanned later in MarkReachableObjects(). |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 298 | DCHECK(generational_ && !whole_heap_collection_ && |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 299 | (space == heap_->GetNonMovingSpace() || space == heap_->GetPrimaryFreeListSpace())) |
| 300 | << "Space " << space->GetName() << " " |
| 301 | << "generational_=" << generational_ << " " |
| 302 | << "whole_heap_collection_=" << whole_heap_collection_ << " "; |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 303 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 308 | class SemiSpaceScanObjectVisitor { |
| 309 | public: |
| 310 | explicit SemiSpaceScanObjectVisitor(SemiSpace* ss) : semi_space_(ss) {} |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 311 | void operator()(Object* obj) const EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, |
| 312 | Locks::heap_bitmap_lock_) { |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 313 | DCHECK(obj != nullptr); |
| 314 | semi_space_->ScanObject(obj); |
| 315 | } |
| 316 | private: |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 317 | SemiSpace* const semi_space_; |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 318 | }; |
| 319 | |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 320 | // Used to verify that there's no references to the from-space. |
| 321 | class SemiSpaceVerifyNoFromSpaceReferencesVisitor { |
| 322 | public: |
| 323 | explicit SemiSpaceVerifyNoFromSpaceReferencesVisitor(space::ContinuousMemMapAllocSpace* from_space) : |
| 324 | from_space_(from_space) {} |
| 325 | |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 326 | void operator()(Object* obj, MemberOffset offset, bool /* is_static */) const |
| 327 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 328 | mirror::Object* ref = obj->GetFieldObject<mirror::Object>(offset); |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 329 | if (from_space_->HasAddress(ref)) { |
| 330 | Runtime::Current()->GetHeap()->DumpObject(LOG(INFO), obj); |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 331 | LOG(FATAL) << ref << " found in from space"; |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 332 | } |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 333 | } |
| 334 | private: |
| 335 | space::ContinuousMemMapAllocSpace* from_space_; |
| 336 | }; |
| 337 | |
| 338 | void SemiSpace::VerifyNoFromSpaceReferences(Object* obj) { |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 339 | DCHECK(!from_space_->HasAddress(obj)) << "Scanning object " << obj << " in from space"; |
| 340 | SemiSpaceVerifyNoFromSpaceReferencesVisitor visitor(from_space_); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 341 | obj->VisitReferences<kMovingClasses>(visitor, VoidFunctor()); |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | class SemiSpaceVerifyNoFromSpaceReferencesObjectVisitor { |
| 345 | public: |
| 346 | explicit SemiSpaceVerifyNoFromSpaceReferencesObjectVisitor(SemiSpace* ss) : semi_space_(ss) {} |
| 347 | void operator()(Object* obj) const |
| 348 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) { |
| 349 | DCHECK(obj != nullptr); |
| 350 | semi_space_->VerifyNoFromSpaceReferences(obj); |
| 351 | } |
| 352 | private: |
| 353 | SemiSpace* const semi_space_; |
| 354 | }; |
| 355 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 356 | void SemiSpace::MarkReachableObjects() { |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 357 | GetTimings()->StartSplit("MarkStackAsLive"); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 358 | accounting::ObjectStack* live_stack = heap_->GetLiveStack(); |
| 359 | heap_->MarkAllocStackAsLive(live_stack); |
| 360 | live_stack->Reset(); |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 361 | |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 362 | GetTimings()->NewSplit("UpdateAndMarkRememberedSets"); |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 363 | for (auto& space : heap_->GetContinuousSpaces()) { |
| 364 | // If the space is immune and has no mod union table (the |
| 365 | // non-moving space when the bump pointer space only collection is |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 366 | // enabled,) then we need to scan its live bitmap or dirty cards as roots |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 367 | // (including the objects on the live stack which have just marked |
| 368 | // in the live bitmap above in MarkAllocStackAsLive().) |
Mathieu Chartier | 8d56210 | 2014-03-12 17:42:10 -0700 | [diff] [blame] | 369 | if (immune_region_.ContainsSpace(space) && |
| 370 | heap_->FindModUnionTableFromSpace(space) == nullptr) { |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 371 | DCHECK(generational_ && !whole_heap_collection_ && |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 372 | (space == GetHeap()->GetNonMovingSpace() || space == GetHeap()->GetPrimaryFreeListSpace())); |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 373 | accounting::RememberedSet* rem_set = heap_->FindRememberedSetFromSpace(space); |
| 374 | if (kUseRememberedSet) { |
| 375 | DCHECK(rem_set != nullptr); |
Hiroshi Yamauchi | 4db7449 | 2014-04-22 17:10:48 -0700 | [diff] [blame] | 376 | rem_set->UpdateAndMarkReferences(MarkHeapReferenceCallback, DelayReferenceReferentCallback, |
| 377 | from_space_, this); |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 378 | if (kIsDebugBuild) { |
| 379 | // Verify that there are no from-space references that |
| 380 | // remain in the space, that is, the remembered set (and the |
| 381 | // card table) didn't miss any from-space references in the |
| 382 | // space. |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 383 | accounting::ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 384 | SemiSpaceVerifyNoFromSpaceReferencesObjectVisitor visitor(this); |
| 385 | live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()), |
| 386 | reinterpret_cast<uintptr_t>(space->End()), |
| 387 | visitor); |
| 388 | } |
| 389 | } else { |
| 390 | DCHECK(rem_set == nullptr); |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 391 | accounting::ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 392 | SemiSpaceScanObjectVisitor visitor(this); |
| 393 | live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()), |
| 394 | reinterpret_cast<uintptr_t>(space->End()), |
| 395 | visitor); |
| 396 | } |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | |
| 400 | if (is_large_object_space_immune_) { |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 401 | GetTimings()->NewSplit("VisitLargeObjects"); |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 402 | DCHECK(generational_ && !whole_heap_collection_); |
Hiroshi Yamauchi | ba5870d | 2014-01-29 15:31:03 -0800 | [diff] [blame] | 403 | // Delay copying the live set to the marked set until here from |
| 404 | // BindBitmaps() as the large objects on the allocation stack may |
| 405 | // be newly added to the live set above in MarkAllocStackAsLive(). |
| 406 | GetHeap()->GetLargeObjectsSpace()->CopyLiveToMarked(); |
| 407 | |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 408 | // When the large object space is immune, we need to scan the |
| 409 | // large object space as roots as they contain references to their |
| 410 | // classes (primitive array classes) that could move though they |
| 411 | // don't contain any other references. |
| 412 | space::LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace(); |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 413 | accounting::LargeObjectBitmap* large_live_bitmap = large_object_space->GetLiveBitmap(); |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 414 | SemiSpaceScanObjectVisitor visitor(this); |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 415 | large_live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(large_object_space->Begin()), |
| 416 | reinterpret_cast<uintptr_t>(large_object_space->End()), |
| 417 | visitor); |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 418 | } |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 419 | GetTimings()->EndSplit(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 420 | // Recursively process the mark stack. |
Mathieu Chartier | 3bb57c7 | 2014-02-18 11:38:45 -0800 | [diff] [blame] | 421 | ProcessMarkStack(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | void SemiSpace::ReclaimPhase() { |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 425 | TimingLogger::ScopedSplit split("ReclaimPhase", GetTimings()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 426 | { |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 427 | WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 428 | // Reclaim unmarked objects. |
| 429 | Sweep(false); |
| 430 | // Swap the live and mark bitmaps for each space which we modified space. This is an |
| 431 | // optimization that enables us to not clear live bits inside of the sweep. Only swaps unbound |
| 432 | // bitmaps. |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 433 | GetTimings()->StartSplit("SwapBitmaps"); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 434 | SwapBitmaps(); |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 435 | GetTimings()->EndSplit(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 436 | // Unbind the live and mark bitmaps. |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 437 | TimingLogger::ScopedSplit split("UnBindBitmaps", GetTimings()); |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 438 | GetHeap()->UnBindBitmaps(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 439 | } |
Mathieu Chartier | ad35d90 | 2014-02-11 16:20:42 -0800 | [diff] [blame] | 440 | if (saved_bytes_ > 0) { |
| 441 | VLOG(heap) << "Avoided dirtying " << PrettySize(saved_bytes_); |
| 442 | } |
Hiroshi Yamauchi | 4b1782f | 2013-12-05 16:46:22 -0800 | [diff] [blame] | 443 | |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 444 | if (generational_) { |
Hiroshi Yamauchi | 4b1782f | 2013-12-05 16:46:22 -0800 | [diff] [blame] | 445 | // Record the end (top) of the to space so we can distinguish |
| 446 | // between objects that were allocated since the last GC and the |
| 447 | // older objects. |
| 448 | last_gc_to_space_end_ = to_space_->End(); |
| 449 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | void SemiSpace::ResizeMarkStack(size_t new_size) { |
| 453 | std::vector<Object*> temp(mark_stack_->Begin(), mark_stack_->End()); |
| 454 | CHECK_LE(mark_stack_->Size(), new_size); |
| 455 | mark_stack_->Resize(new_size); |
| 456 | for (const auto& obj : temp) { |
| 457 | mark_stack_->PushBack(obj); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | inline void SemiSpace::MarkStackPush(Object* obj) { |
| 462 | if (UNLIKELY(mark_stack_->Size() >= mark_stack_->Capacity())) { |
| 463 | ResizeMarkStack(mark_stack_->Capacity() * 2); |
| 464 | } |
| 465 | // The object must be pushed on to the mark stack. |
| 466 | mark_stack_->PushBack(obj); |
| 467 | } |
| 468 | |
Mathieu Chartier | ad35d90 | 2014-02-11 16:20:42 -0800 | [diff] [blame] | 469 | static inline size_t CopyAvoidingDirtyingPages(void* dest, const void* src, size_t size) { |
| 470 | if (LIKELY(size <= static_cast<size_t>(kPageSize))) { |
| 471 | // We will dirty the current page and somewhere in the middle of the next page. This means |
| 472 | // that the next object copied will also dirty that page. |
| 473 | // TODO: Worth considering the last object copied? We may end up dirtying one page which is |
| 474 | // not necessary per GC. |
| 475 | memcpy(dest, src, size); |
| 476 | return 0; |
| 477 | } |
| 478 | size_t saved_bytes = 0; |
| 479 | byte* byte_dest = reinterpret_cast<byte*>(dest); |
| 480 | if (kIsDebugBuild) { |
| 481 | for (size_t i = 0; i < size; ++i) { |
| 482 | CHECK_EQ(byte_dest[i], 0U); |
| 483 | } |
| 484 | } |
| 485 | // Process the start of the page. The page must already be dirty, don't bother with checking. |
| 486 | const byte* byte_src = reinterpret_cast<const byte*>(src); |
| 487 | const byte* limit = byte_src + size; |
| 488 | size_t page_remain = AlignUp(byte_dest, kPageSize) - byte_dest; |
| 489 | // Copy the bytes until the start of the next page. |
| 490 | memcpy(dest, src, page_remain); |
| 491 | byte_src += page_remain; |
| 492 | byte_dest += page_remain; |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 493 | DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_dest), kPageSize); |
| 494 | DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_dest), sizeof(uintptr_t)); |
| 495 | DCHECK_ALIGNED(reinterpret_cast<uintptr_t>(byte_src), sizeof(uintptr_t)); |
Mathieu Chartier | ad35d90 | 2014-02-11 16:20:42 -0800 | [diff] [blame] | 496 | while (byte_src + kPageSize < limit) { |
| 497 | bool all_zero = true; |
| 498 | uintptr_t* word_dest = reinterpret_cast<uintptr_t*>(byte_dest); |
| 499 | const uintptr_t* word_src = reinterpret_cast<const uintptr_t*>(byte_src); |
| 500 | for (size_t i = 0; i < kPageSize / sizeof(*word_src); ++i) { |
| 501 | // Assumes the destination of the copy is all zeros. |
| 502 | if (word_src[i] != 0) { |
| 503 | all_zero = false; |
| 504 | word_dest[i] = word_src[i]; |
| 505 | } |
| 506 | } |
| 507 | if (all_zero) { |
| 508 | // Avoided copying into the page since it was all zeros. |
| 509 | saved_bytes += kPageSize; |
| 510 | } |
| 511 | byte_src += kPageSize; |
| 512 | byte_dest += kPageSize; |
| 513 | } |
| 514 | // Handle the part of the page at the end. |
| 515 | memcpy(byte_dest, byte_src, limit - byte_src); |
| 516 | return saved_bytes; |
| 517 | } |
| 518 | |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 519 | mirror::Object* SemiSpace::MarkNonForwardedObject(mirror::Object* obj) { |
| 520 | size_t object_size = obj->SizeOf(); |
Mathieu Chartier | 5dc08a6 | 2014-01-10 10:10:23 -0800 | [diff] [blame] | 521 | size_t bytes_allocated; |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 522 | mirror::Object* forward_address = nullptr; |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 523 | if (generational_ && reinterpret_cast<byte*>(obj) < last_gc_to_space_end_) { |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 524 | // If it's allocated before the last GC (older), move |
| 525 | // (pseudo-promote) it to the main free list space (as sort |
| 526 | // of an old generation.) |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 527 | space::MallocSpace* promo_dest_space = GetHeap()->GetPrimaryFreeListSpace(); |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 528 | forward_address = promo_dest_space->AllocThreadUnsafe(self_, object_size, &bytes_allocated, |
| 529 | nullptr); |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 530 | if (UNLIKELY(forward_address == nullptr)) { |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 531 | // If out of space, fall back to the to-space. |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 532 | forward_address = to_space_->AllocThreadUnsafe(self_, object_size, &bytes_allocated, nullptr); |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 533 | } else { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 534 | bytes_promoted_ += bytes_allocated; |
Hiroshi Yamauchi | 38e68e9 | 2014-03-07 13:59:08 -0800 | [diff] [blame] | 535 | // Dirty the card at the destionation as it may contain |
| 536 | // references (including the class pointer) to the bump pointer |
| 537 | // space. |
| 538 | GetHeap()->WriteBarrierEveryFieldOf(forward_address); |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 539 | // Handle the bitmaps marking. |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 540 | accounting::ContinuousSpaceBitmap* live_bitmap = promo_dest_space->GetLiveBitmap(); |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 541 | DCHECK(live_bitmap != nullptr); |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 542 | accounting::ContinuousSpaceBitmap* mark_bitmap = promo_dest_space->GetMarkBitmap(); |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 543 | DCHECK(mark_bitmap != nullptr); |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 544 | DCHECK(!live_bitmap->Test(forward_address)); |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 545 | if (!whole_heap_collection_) { |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 546 | // If collecting the bump pointer spaces only, live_bitmap == mark_bitmap. |
| 547 | DCHECK_EQ(live_bitmap, mark_bitmap); |
| 548 | |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 549 | // If a bump pointer space only collection, delay the live |
| 550 | // bitmap marking of the promoted object until it's popped off |
| 551 | // the mark stack (ProcessMarkStack()). The rationale: we may |
| 552 | // be in the middle of scanning the objects in the promo |
| 553 | // destination space for |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 554 | // non-moving-space-to-bump-pointer-space references by |
| 555 | // iterating over the marked bits of the live bitmap |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 556 | // (MarkReachableObjects()). If we don't delay it (and instead |
| 557 | // mark the promoted object here), the above promo destination |
| 558 | // space scan could encounter the just-promoted object and |
| 559 | // forward the references in the promoted object's fields even |
| 560 | // through it is pushed onto the mark stack. If this happens, |
| 561 | // the promoted object would be in an inconsistent state, that |
| 562 | // is, it's on the mark stack (gray) but its fields are |
| 563 | // already forwarded (black), which would cause a |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 564 | // DCHECK(!to_space_->HasAddress(obj)) failure below. |
| 565 | } else { |
| 566 | // Mark forward_address on the live bit map. |
| 567 | live_bitmap->Set(forward_address); |
| 568 | // Mark forward_address on the mark bit map. |
| 569 | DCHECK(!mark_bitmap->Test(forward_address)); |
| 570 | mark_bitmap->Set(forward_address); |
| 571 | } |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 572 | } |
| 573 | DCHECK(forward_address != nullptr); |
| 574 | } else { |
| 575 | // If it's allocated after the last GC (younger), copy it to the to-space. |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 576 | forward_address = to_space_->AllocThreadUnsafe(self_, object_size, &bytes_allocated, nullptr); |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 577 | } |
Hiroshi Yamauchi | 13bf2e6 | 2014-05-19 12:49:45 -0700 | [diff] [blame] | 578 | CHECK(forward_address != nullptr) << "Out of memory in the to-space."; |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 579 | ++objects_moved_; |
| 580 | bytes_moved_ += bytes_allocated; |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 581 | // Copy over the object and add it to the mark stack since we still need to update its |
| 582 | // references. |
Mathieu Chartier | ad35d90 | 2014-02-11 16:20:42 -0800 | [diff] [blame] | 583 | saved_bytes_ += |
| 584 | CopyAvoidingDirtyingPages(reinterpret_cast<void*>(forward_address), obj, object_size); |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 585 | if (kUseBakerOrBrooksReadBarrier) { |
| 586 | obj->AssertReadBarrierPointer(); |
| 587 | if (kUseBrooksReadBarrier) { |
| 588 | DCHECK_EQ(forward_address->GetReadBarrierPointer(), obj); |
| 589 | forward_address->SetReadBarrierPointer(forward_address); |
| 590 | } |
| 591 | forward_address->AssertReadBarrierPointer(); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 592 | } |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 593 | if (to_space_live_bitmap_ != nullptr) { |
| 594 | to_space_live_bitmap_->Set(forward_address); |
| 595 | } |
Mathieu Chartier | 5dc08a6 | 2014-01-10 10:10:23 -0800 | [diff] [blame] | 596 | DCHECK(to_space_->HasAddress(forward_address) || |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 597 | (generational_ && GetHeap()->GetPrimaryFreeListSpace()->HasAddress(forward_address))); |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 598 | return forward_address; |
| 599 | } |
| 600 | |
Mathieu Chartier | 3bb57c7 | 2014-02-18 11:38:45 -0800 | [diff] [blame] | 601 | void SemiSpace::ProcessMarkStackCallback(void* arg) { |
Mathieu Chartier | 3bb57c7 | 2014-02-18 11:38:45 -0800 | [diff] [blame] | 602 | reinterpret_cast<SemiSpace*>(arg)->ProcessMarkStack(); |
| 603 | } |
| 604 | |
| 605 | mirror::Object* SemiSpace::MarkObjectCallback(mirror::Object* root, void* arg) { |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 606 | auto ref = StackReference<mirror::Object>::FromMirrorPtr(root); |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 607 | reinterpret_cast<SemiSpace*>(arg)->MarkObject(&ref); |
| 608 | return ref.AsMirrorPtr(); |
| 609 | } |
| 610 | |
| 611 | void SemiSpace::MarkHeapReferenceCallback(mirror::HeapReference<mirror::Object>* obj_ptr, |
| 612 | void* arg) { |
| 613 | reinterpret_cast<SemiSpace*>(arg)->MarkObject(obj_ptr); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 614 | } |
| 615 | |
Hiroshi Yamauchi | 4db7449 | 2014-04-22 17:10:48 -0700 | [diff] [blame] | 616 | void SemiSpace::DelayReferenceReferentCallback(mirror::Class* klass, mirror::Reference* ref, |
| 617 | void* arg) { |
| 618 | reinterpret_cast<SemiSpace*>(arg)->DelayReferenceReferent(klass, ref); |
| 619 | } |
| 620 | |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 621 | void SemiSpace::MarkRootCallback(Object** root, void* arg, uint32_t /*thread_id*/, |
| 622 | RootType /*root_type*/) { |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 623 | auto ref = StackReference<mirror::Object>::FromMirrorPtr(*root); |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 624 | reinterpret_cast<SemiSpace*>(arg)->MarkObject(&ref); |
| 625 | if (*root != ref.AsMirrorPtr()) { |
| 626 | *root = ref.AsMirrorPtr(); |
| 627 | } |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 628 | } |
| 629 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 630 | // Marks all objects in the root set. |
| 631 | void SemiSpace::MarkRoots() { |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 632 | GetTimings()->NewSplit("MarkRoots"); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 633 | // TODO: Visit up image roots as well? |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 634 | Runtime::Current()->VisitRoots(MarkRootCallback, this); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 635 | } |
| 636 | |
Mathieu Chartier | 308351a | 2014-06-15 12:39:02 -0700 | [diff] [blame] | 637 | bool SemiSpace::HeapReferenceMarkedCallback(mirror::HeapReference<mirror::Object>* object, |
| 638 | void* arg) { |
| 639 | mirror::Object* obj = object->AsMirrorPtr(); |
| 640 | mirror::Object* new_obj = |
| 641 | reinterpret_cast<SemiSpace*>(arg)->GetMarkedForwardAddress(obj); |
| 642 | if (new_obj == nullptr) { |
| 643 | return false; |
| 644 | } |
| 645 | if (new_obj != obj) { |
| 646 | // Write barrier is not necessary since it still points to the same object, just at a different |
| 647 | // address. |
| 648 | object->Assign(new_obj); |
| 649 | } |
| 650 | return true; |
| 651 | } |
| 652 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 653 | mirror::Object* SemiSpace::MarkedForwardingAddressCallback(mirror::Object* object, void* arg) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 654 | return reinterpret_cast<SemiSpace*>(arg)->GetMarkedForwardAddress(object); |
| 655 | } |
| 656 | |
| 657 | void SemiSpace::SweepSystemWeaks() { |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 658 | GetTimings()->StartSplit("SweepSystemWeaks"); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 659 | Runtime::Current()->SweepSystemWeaks(MarkedForwardingAddressCallback, this); |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 660 | GetTimings()->EndSplit(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 663 | bool SemiSpace::ShouldSweepSpace(space::ContinuousSpace* space) const { |
Mathieu Chartier | 8d56210 | 2014-03-12 17:42:10 -0700 | [diff] [blame] | 664 | return space != from_space_ && space != to_space_ && !immune_region_.ContainsSpace(space); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | void SemiSpace::Sweep(bool swap_bitmaps) { |
| 668 | DCHECK(mark_stack_->IsEmpty()); |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 669 | TimingLogger::ScopedSplit split("Sweep", GetTimings()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 670 | for (const auto& space : GetHeap()->GetContinuousSpaces()) { |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 671 | if (space->IsContinuousMemMapAllocSpace()) { |
| 672 | space::ContinuousMemMapAllocSpace* alloc_space = space->AsContinuousMemMapAllocSpace(); |
| 673 | if (!ShouldSweepSpace(alloc_space)) { |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 674 | continue; |
| 675 | } |
Mathieu Chartier | ec05007 | 2014-01-07 16:00:07 -0800 | [diff] [blame] | 676 | TimingLogger::ScopedSplit split( |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 677 | alloc_space->IsZygoteSpace() ? "SweepZygoteSpace" : "SweepAllocSpace", GetTimings()); |
| 678 | RecordFree(alloc_space->Sweep(swap_bitmaps)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 679 | } |
| 680 | } |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 681 | if (!is_large_object_space_immune_) { |
| 682 | SweepLargeObjects(swap_bitmaps); |
| 683 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | void SemiSpace::SweepLargeObjects(bool swap_bitmaps) { |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 687 | DCHECK(!is_large_object_space_immune_); |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 688 | TimingLogger::ScopedSplit split("SweepLargeObjects", GetTimings()); |
| 689 | RecordFreeLOS(heap_->GetLargeObjectsSpace()->Sweep(swap_bitmaps)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | // Process the "referent" field in a java.lang.ref.Reference. If the referent has not yet been |
| 693 | // marked, put it on the appropriate list in the heap for later processing. |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 694 | void SemiSpace::DelayReferenceReferent(mirror::Class* klass, mirror::Reference* reference) { |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 695 | heap_->GetReferenceProcessor()->DelayReferenceReferent(klass, reference, |
Mathieu Chartier | 308351a | 2014-06-15 12:39:02 -0700 | [diff] [blame] | 696 | &HeapReferenceMarkedCallback, this); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 697 | } |
| 698 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 699 | class SemiSpaceMarkObjectVisitor { |
| 700 | public: |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 701 | explicit SemiSpaceMarkObjectVisitor(SemiSpace* collector) : collector_(collector) { |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 702 | } |
| 703 | |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 704 | void operator()(Object* obj, MemberOffset offset, bool /* is_static */) const ALWAYS_INLINE |
| 705 | EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
Mathieu Chartier | 580a8df | 2014-03-26 15:15:57 -0700 | [diff] [blame] | 706 | // Object was already verified when we scanned it. |
| 707 | collector_->MarkObject(obj->GetFieldObjectReferenceAddr<kVerifyNone>(offset)); |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 708 | } |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 709 | |
| 710 | void operator()(mirror::Class* klass, mirror::Reference* ref) const |
| 711 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 712 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
| 713 | collector_->DelayReferenceReferent(klass, ref); |
| 714 | } |
| 715 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 716 | private: |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 717 | SemiSpace* const collector_; |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 718 | }; |
| 719 | |
| 720 | // Visit all of the references of an object and update. |
| 721 | void SemiSpace::ScanObject(Object* obj) { |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 722 | DCHECK(!from_space_->HasAddress(obj)) << "Scanning object " << obj << " in from space"; |
| 723 | SemiSpaceMarkObjectVisitor visitor(this); |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 724 | obj->VisitReferences<kMovingClasses>(visitor, visitor); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | // Scan anything that's on the mark stack. |
Mathieu Chartier | 3bb57c7 | 2014-02-18 11:38:45 -0800 | [diff] [blame] | 728 | void SemiSpace::ProcessMarkStack() { |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 729 | space::MallocSpace* promo_dest_space = nullptr; |
| 730 | accounting::ContinuousSpaceBitmap* live_bitmap = nullptr; |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 731 | if (generational_ && !whole_heap_collection_) { |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 732 | // If a bump pointer space only collection (and the promotion is |
| 733 | // enabled,) we delay the live-bitmap marking of promoted objects |
| 734 | // from MarkObject() until this function. |
| 735 | promo_dest_space = GetHeap()->GetPrimaryFreeListSpace(); |
| 736 | live_bitmap = promo_dest_space->GetLiveBitmap(); |
| 737 | DCHECK(live_bitmap != nullptr); |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 738 | accounting::ContinuousSpaceBitmap* mark_bitmap = promo_dest_space->GetMarkBitmap(); |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 739 | DCHECK(mark_bitmap != nullptr); |
| 740 | DCHECK_EQ(live_bitmap, mark_bitmap); |
| 741 | } |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 742 | GetTimings()->StartSplit("ProcessMarkStack"); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 743 | while (!mark_stack_->IsEmpty()) { |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 744 | Object* obj = mark_stack_->PopBack(); |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 745 | if (generational_ && !whole_heap_collection_ && promo_dest_space->HasAddress(obj)) { |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 746 | // obj has just been promoted. Mark the live bitmap for it, |
| 747 | // which is delayed from MarkObject(). |
| 748 | DCHECK(!live_bitmap->Test(obj)); |
| 749 | live_bitmap->Set(obj); |
| 750 | } |
| 751 | ScanObject(obj); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 752 | } |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 753 | GetTimings()->EndSplit(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 754 | } |
| 755 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 756 | inline Object* SemiSpace::GetMarkedForwardAddress(mirror::Object* obj) const |
| 757 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
| 758 | // All immune objects are assumed marked. |
Mathieu Chartier | 8d56210 | 2014-03-12 17:42:10 -0700 | [diff] [blame] | 759 | if (immune_region_.ContainsObject(obj)) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 760 | return obj; |
| 761 | } |
| 762 | if (from_space_->HasAddress(obj)) { |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 763 | // Returns either the forwarding address or nullptr. |
| 764 | return GetForwardingAddressInFromSpace(obj); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 765 | } else if (to_space_->HasAddress(obj)) { |
Mathieu Chartier | 85a43c0 | 2014-01-07 17:59:00 -0800 | [diff] [blame] | 766 | // Should be unlikely. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 767 | // Already forwarded, must be marked. |
| 768 | return obj; |
| 769 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 770 | return mark_bitmap_->Test(obj) ? obj : nullptr; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 771 | } |
| 772 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 773 | void SemiSpace::SetToSpace(space::ContinuousMemMapAllocSpace* to_space) { |
| 774 | DCHECK(to_space != nullptr); |
| 775 | to_space_ = to_space; |
| 776 | } |
| 777 | |
| 778 | void SemiSpace::SetFromSpace(space::ContinuousMemMapAllocSpace* from_space) { |
| 779 | DCHECK(from_space != nullptr); |
| 780 | from_space_ = from_space; |
| 781 | } |
| 782 | |
| 783 | void SemiSpace::FinishPhase() { |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 784 | TimingLogger::ScopedSplit split("FinishPhase", GetTimings()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 785 | // Null the "to" and "from" spaces since compacting from one to the other isn't valid until |
| 786 | // further action is done by the heap. |
| 787 | to_space_ = nullptr; |
| 788 | from_space_ = nullptr; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 789 | CHECK(mark_stack_->IsEmpty()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 790 | mark_stack_->Reset(); |
Hiroshi Yamauchi | 6f4ffe4 | 2014-01-13 12:30:44 -0800 | [diff] [blame] | 791 | if (generational_) { |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 792 | // Decide whether to do a whole heap collection or a bump pointer |
| 793 | // only space collection at the next collection by updating |
Hiroshi Yamauchi | df386c5 | 2014-04-08 16:21:52 -0700 | [diff] [blame] | 794 | // whole_heap_collection. |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 795 | if (!whole_heap_collection_) { |
Hiroshi Yamauchi | 24faeb2 | 2014-05-07 13:12:43 -0700 | [diff] [blame] | 796 | // Enable whole_heap_collection if the bytes promoted since the |
| 797 | // last whole heap collection or the large object bytes |
| 798 | // allocated exceeds a threshold. |
| 799 | bytes_promoted_since_last_whole_heap_collection_ += bytes_promoted_; |
| 800 | bool bytes_promoted_threshold_exceeded = |
| 801 | bytes_promoted_since_last_whole_heap_collection_ >= kBytesPromotedThreshold; |
| 802 | uint64_t current_los_bytes_allocated = GetHeap()->GetLargeObjectsSpace()->GetBytesAllocated(); |
| 803 | uint64_t last_los_bytes_allocated = |
| 804 | large_object_bytes_allocated_at_last_whole_heap_collection_; |
| 805 | bool large_object_bytes_threshold_exceeded = |
| 806 | current_los_bytes_allocated >= |
| 807 | last_los_bytes_allocated + kLargeObjectBytesAllocatedThreshold; |
| 808 | if (bytes_promoted_threshold_exceeded || large_object_bytes_threshold_exceeded) { |
| 809 | whole_heap_collection_ = true; |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 810 | } |
| 811 | } else { |
Hiroshi Yamauchi | 24faeb2 | 2014-05-07 13:12:43 -0700 | [diff] [blame] | 812 | // Reset the counters. |
| 813 | bytes_promoted_since_last_whole_heap_collection_ = bytes_promoted_; |
| 814 | large_object_bytes_allocated_at_last_whole_heap_collection_ = |
| 815 | GetHeap()->GetLargeObjectsSpace()->GetBytesAllocated(); |
| 816 | whole_heap_collection_ = false; |
Hiroshi Yamauchi | 05e713a | 2014-01-09 13:24:51 -0800 | [diff] [blame] | 817 | } |
| 818 | } |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 819 | // Clear all of the spaces' mark bitmaps. |
| 820 | WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 821 | heap_->ClearMarkedObjects(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 822 | } |
| 823 | |
Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 824 | void SemiSpace::RevokeAllThreadLocalBuffers() { |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 825 | GetTimings()->StartSplit("(Paused)RevokeAllThreadLocalBuffers"); |
Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 826 | GetHeap()->RevokeAllThreadLocalBuffers(); |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 827 | GetTimings()->EndSplit(); |
Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 828 | } |
| 829 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 830 | } // namespace collector |
| 831 | } // namespace gc |
| 832 | } // namespace art |