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