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