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